2012-02-25 78 views
0

这里是我的类它进入无限循环,请检查我要去哪里错了...我试图让id的图像视图使它随机,然后试图设置文字视图与imageview的描述试图设置文本视图

public class Object { 

int ObectIds[]; 
LinearLayout HUDDisplay; 
int HudDisplayText[] = {R.id.HUD_Text_Element1, 
     R.id.HUD_Text_Element2, 
     R.id.HUD_Text_Element3, 
     R.id.HUD_Text_Element4, 
     R.id.HUD_Text_Element5, 
     R.id.HUD_Text_Element6, 
     R.id.HUD_Text_Element7}; 

TextView[] text; 
View v; 

Object(Context context,View vs) { 
    super(); 
    ObectIds = new int[8]; 
    HUDDisplay=(LinearLayout)vs.findViewById(R.id.HUD_Display); 

    for (int i = 0; i < 8; i++) { 
     ObectIds[i] = (R.id.imageView1) + i; 
     Log.d("ImageView", "Image Id's " + ObectIds[i]); 

    } 

    randomize(vs); 
    setTextView(); 

} 

public void setTextView() 
{ 
    for(int i=0;i<8;++i) 
    { 
     text[i] =(TextView) HUDDisplay.findViewById(HudDisplayText[i]); 

     text[i].setText(v.getContentDescription()); 
    } 
} 

public void randomize(View vs) { 
    for (int i = 0; i < 8; i++) { 
     while (true) { 
      shuffleArray(ObectIds); 
     v = vs.findViewById(ObectIds[i]); 
      Log.d("Image", "Image Id's " + v.getId()); 
      if (!v.isClickable()) { 
       v.setClickable(true); 
       break; 
      } 
     } 
    } 
} 

static void shuffleArray(int[] ar) { 
    Random rnd = new Random(); 
    for (int i = ar.length - 1; i >= 0; i--) { 
     int index = rnd.nextInt(i + 1); 
     // Simple swap 
     int a = ar[index]; 
     ar[index] = ar[i]; 
     ar[i] = a; 
    } 
} 

}

回答

0

你有你只有突破如果v无法点击一段时间(true)循环。如果v是可点击的,会发生什么?代码中的任何内容都不会将v设置为不可点击,默认情况下视图不可点击。

0

我注意到你正在使用Object类。对象基本上是所有类扩展的根源。如果你在构造函数中调用super(),它会调用超类构造函数,这也是Object ...这可能是问题所在。

尝试寻找有关如何从Java/Android开始的教程,因为您也在使用不推荐的变量名称。例如。在Java中,: - 一类具有资本 启动 - - 一个变量,以小写 启动功能与小写开头:

public class Captial 
{ 
    private int anIntegerStartsWithLowerCase; 

    private void functionsAreLowerCaseAsWell() 
    { 
    } 
} 

也看看你的循环......它看起来是上房

1

嗨,哥们我看到你的代码&发现错误代码:

请比较下面与您的代码代码...构造

for (int i = 0; i < 8; i++) { 
     ObectIds[i] = **HudDisplayText[i]**; 
     Log.d("ImageView", "Image Id's " + ObectIds[i]); 

    }