2014-05-07 9 views
-2

我试图让从Arraylist随机值,然后将其存储在Buttons。 每当我运行此代码与buttonListRbra我的应用程序崩溃。如何从Arraylist中获取值到按钮?

这是我的代码:

private void generateRandomNumbersRbra() 
{ 
    int rnd; 
    Random rand = new Random(); 
    int[] randomPick = new int[8]; 
    ArrayList<Integer> numbersRbra = new ArrayList<Integer>();  
    Button[] buttonListRbra = new Button[8]; 
    buttonListRbra[0] = (Button)findViewById(R.id.tap1); 
    buttonListRbra[1] = (Button)findViewById(R.id.tap2); 
    buttonListRbra[2] = (Button)findViewById(R.id.tap3); 
    buttonListRbra[3] = (Button)findViewById(R.id.tap4); 
    buttonListRbra[4] = (Button)findViewById(R.id.tap5); 
    buttonListRbra[5] = (Button)findViewById(R.id.tap6); 
    buttonListRbra[6] = (Button)findViewById(R.id.tap7); 
    buttonListRbra[7] = (Button)findViewById(R.id.tap8); 
    buttonListRbra[8] = (Button)findViewById(R.id.tap9);  
    for (int i=0 ; i< 8;i++) 
    { 
    rnd = rand.nextInt(8) + 1; 

    if(i==0) 
    { 
    randomPick[0] = rnd; 
    numbersRbra.add(randomPick[0]); 
    } 
    else 
    { 
    while(numbersRbra.contains(new Integer(rnd))) 
    { 
    rnd = rand.nextInt(8) + 1; 
    } 
    randomPick[i] = rnd; 
    numbersRbra.add(randomPick[i]); 
    } 
    } 
} 

logcat的:

05-07 14:15:50.325:d/libEGL(10749):加载/系统/ LIB/EGL/libEGL_mali.so 05-07 14:15:50.355:D/libEGL(10749):loaded /system/lib/egl/libGLESv1_CM_mali.so 05-07 14:15:50.360:D/libEGL(10749):loaded /系统/ LIB/EGL/libGLESv2_mali.so 05-07 14:15:50.370:E /(10749):设备驱动程序API匹配 05-07 14:15:50.370:E /(10749):设备驱动程序API版本:23 05-07 14:15:50.370:E /(10749):用户空间API版本:23 05-07 14: 15:50.370:E /(10749):mali:REVISION = Linux-r3p2-01rel3 BUILD_DATE = Wed Oct 9 21:05:57 KST 2013 05-07 14:15:50.490:D/OpenGLRenderer(10749):启用调试模式0 05-07 14:15:54.010:D/AndroidRuntime(10749):关闭VM 05-07 14:15:54.010:W/dalvikvm(10749):threadid = 1:线程退出时未捕获异常= 0x41bdc700) 05-07 14:15:54.015:E/AndroidRuntime(10749):致命例外:main 05-07 14:15:54.015:E/AndroidRuntime(10749):java.lang.IllegalStateException:无法执行方法的活动 05-07 14:15:54.015:E/AndroidRuntime(10749):在android.view.View $ 1.onClick(View.j ava:3838) 05-07 14:15:54.015:E/AndroidRuntime(10749):at android.view.View.performClick(View.java:4475) 05-07 14:15:54.015:E/AndroidRuntime 10749):at android.view.View $ PerformClick.run(View.java:18786) 05-07 14:15:54.015:E/AndroidRuntime(10749):at android.os.Handler.handleCallback(Handler.java: 730/012R05-07 14:15:54.015:E/AndroidRuntime(10749) :at android.os.Looper.loop(Looper.java:176) 05-07 14:15:54.015:E/AndroidRuntime(10749):at android.app.ActivityThread.main(ActivityThread.java:5419) 05 -07 14:15:54.015:E/AndroidRuntime(10749):在java.lang.reflect.Method.invokeNative(本机方法) 05-07 14:15:54.015:E/AndroidRuntime(10749):在java.lang.reflect.Method.invoke(Method.java:525) 05-07 14:15:54.015:E/AndroidRuntime(10749):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit .java:1046) 05-07 14:15:54.015:E/AndroidRuntime(10749):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 05-07 14:15:54.015 :E/AndroidRuntime(10749):at dalvik.system.NativeStart.main(Native Method) 05-07 14:15:54.015:E/AndroidRuntime(10749):引起:java.lang.reflect.InvocationTargetException 05- 07 14:15:54.015:E/AndroidRuntime(10749):在java.lang.reflect.Method.invokeNative(本地方法) 05-07 14:15:54.015:E/AndroidRuntime(10749):at java.lang。反射。方法.invoke(Method.java:525) 05-07 14:15:54.015:E/AndroidRuntime(10749):在android.view.View $ 1.onClick(View.java:3833) 05-07 14:15:54.015:E/AndroidRuntime(10749):... 11更多 05-07 14:15:54.015:E/AndroidRuntime(10749):导致:java.lang.ArrayIndexOutOfBoundsException:length = 8; index = 8 05-07 14:15:54.015:E/AndroidRuntime(10749):at com.roc.quickynumbers.GameActivity.generateRandomNumbersRbra(GameActivity.java:73) 05-07 14:15:54.015:E/AndroidRuntime (10749):在com.roc.quickynumbers.GameActivity.startGame(GameActivity.java:46) 05-07 14:15:54.015:E/AndroidRuntime(10749):...14多

提前感谢!

编辑:

谢谢大家。我现在开始工作了!

解决方案:

int rnd; 
    Random rand = new Random(); 
    ArrayList<Integer> numbersRbra = new ArrayList<Integer>(); 

    Button[] buttonListRbra = new Button[9]; 
    buttonListRbra[0] = (Button)findViewById(R.id.tap1); 
    buttonListRbra[1] = (Button)findViewById(R.id.tap2); 
    buttonListRbra[2] = (Button)findViewById(R.id.tap3); 
    buttonListRbra[3] = (Button)findViewById(R.id.tap4); 
    buttonListRbra[4] = (Button)findViewById(R.id.tap5); 
    buttonListRbra[5] = (Button)findViewById(R.id.tap6); 
    buttonListRbra[6] = (Button)findViewById(R.id.tap7); 
    buttonListRbra[7] = (Button)findViewById(R.id.tap8); 
    buttonListRbra[8] = (Button)findViewById(R.id.tap9); 

    for (int i=0; i < 9; i++) // Loop through your entire list to access all your 9 Buttons 
    { 
     rnd = rand.nextInt(9) + 1; // Return an int in the range [1,9] => 9 elements 

     if(i==0) 
     { 
      numbersRbra.add(rnd); 
      buttonListRbra[i].setText(numbersRbra.get(i).toString()); //Set the Button Text for the first iteration too 
     } 
     else 
     { 
      while(numbersRbra.contains(rnd)) // Transformation between Integer and int done automatically 
      { 
       rnd = rand.nextInt(9) + 1; // Return an int in the range [1,9] 
      } 
      numbersRbra.add(rnd); 
      buttonListRbra[i].setText(numbersRbra.get(i).toString()); // Set it after finding the correct number 
     } 
     } 
+0

发布您的logcat。 – prakash

+1

具有8个元素的数组不能包含9个元素,请检查大小 – gerbit

回答

1

你没有正确理解Array

当你创建了一个数组:new Button[8];

它可以包含8个元素。分配的阵列将开始在索引0和索引7 停止所以,如果你想存储9个元素像你一样,做这样的事情:正确通过它

Button[] buttonListRbra = new Button[9]; 

环。

的logcat中证实了这一错误。你有一个ArrayIndexOutOfBoundsException。您正试图访问一个大于数组大小的索引。检查this以自行检测来自LogCat的错误。

编辑:

以下是更正后的代码,我想你想:

 int rnd; 
     Random rand = new Random(); 
     ArrayList<Integer> numbersRbra = new ArrayList<Integer>(); 

     Button[] buttonListRbra = new Button[9]; 
     buttonListRbra[0] = (Button)findViewById(R.id.tap1); 
     buttonListRbra[1] = (Button)findViewById(R.id.tap2); 
     buttonListRbra[2] = (Button)findViewById(R.id.tap3); 
     buttonListRbra[3] = (Button)findViewById(R.id.tap4); 
     buttonListRbra[4] = (Button)findViewById(R.id.tap5); 
     buttonListRbra[5] = (Button)findViewById(R.id.tap6); 
     buttonListRbra[6] = (Button)findViewById(R.id.tap7); 
     buttonListRbra[7] = (Button)findViewById(R.id.tap8); 
     buttonListRbra[8] = (Button)findViewById(R.id.tap9); 

     for (int i=0; i < 9; i++) // Loop through your entire list to access all your 9 Buttons 
     { 
      rnd = rand.nextInt(9) + 1; // Return an int in the range [1,9] => 9 elements 

      if(i==0) 
      { 
       numbersRbra.add(rnd); 
       buttonListRbra[i].setText(numbersRbra.get(i)); // Set the Button Text for the first iteration too 
      } 
      else 
      { 
       while(numbersRbra.contains(rnd)) // Transformation between Integer and int done automatically 
       { 
        rnd = rand.nextInt(9) + 1; // Return an int in the range [1,9] 
       } 
       numbersRbra.add(rnd); 
       buttonListRbra[i].setText(numbersRbra.get(i)); // Set it after finding the correct number 
      } 
     } 

我强烈建议你理解的代码首先使用它之前,了解一些关于如何使用ArrayList。正如我在第一个答案中已经说过的,您应该尝试了解LogCat为您提供的错误。它只能帮助你在未来的应用程序中。

+0

谢谢你修正了错误。我做了randomPick [8],并设置相同的号码到新的按钮[8](现在9) – daxcyy

+0

@daxcyy我犯了错误我更新了我的答案,使您的代码功能。希望你能理解它,并从中学习:) – calimbak

+0

非常感谢!我现在正在工作,但我不得不添加一些代码。我添加了:.toString()把它放在mu按钮中,我会发布上面的解决方案! – daxcyy

0

您正在将int(原始数据类型)添加到Integer(Hullclass)的List中。 尝试加入Integer.valueof(randomPick[i])。 下一次,请在您的文章中包含Logcat输出,以便我们可以读取堆栈跟踪。 另外,我认为你不需要你使用的数组。只需立即将值插入列表中。

编辑:以上职位属权,你也可以通过访问数组里面只有8场的长场9创建ArrayIndexOutOfBoundsException。 你应该考虑阅读错误日志,以便下次自己找到这样的错误。

编辑: 你基本上是做

randomPick[i] = rnd; numbersRbra.add(randomPick[i]);

而是你可以

numbersRbra.add(rnd);

你不有色的randomPick[]其他地方,因此被填充阵列和一个列表具有相同的随机值没有特别的原因(我猜)。

如果您随后想将按钮的标签设置为生成的随机数,则您只需拨打buttonListRbra[i].setText(numbersRbra.get(i));即可 。

+0

你是什么意思我不需要数组列表?和我发布我的logcat – daxcyy

+0

我已编辑我的帖子来回答你的问题。 – schubakah

+0

谢谢,但我现在如何设置arraylist的值到我的按钮,即时通讯不好与arraylists,因为你已经注意到 – daxcyy

0
Button[] buttonListRbra = new Button[8]; 
    buttonListRbra[0] = (Button)findViewById(R.id.tap1); 
    buttonListRbra[1] = (Button)findViewById(R.id.tap2); 
    buttonListRbra[2] = (Button)findViewById(R.id.tap3); 
    buttonListRbra[3] = (Button)findViewById(R.id.tap4); 
    buttonListRbra[4] = (Button)findViewById(R.id.tap5); 
    buttonListRbra[5] = (Button)findViewById(R.id.tap6); 
    buttonListRbra[6] = (Button)findViewById(R.id.tap7); 
    buttonListRbra[7] = (Button)findViewById(R.id.tap8); 
    buttonListRbra[8] = (Button)findViewById(R.id.tap9); 

您正在创建一个可以保存到7索引0 8个键引用数组,之后你想在8索引来访问按钮,但指数8不存在,也就是说,它是出界从阵列的角度来看,所以你在尝试执行buttonListRbra[8]时会遇到异常。