我試圖讓從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
}
}
發佈您的logcat。 – prakash
具有8個元素的數組不能包含9個元素,請檢查大小 – gerbit