2014-05-07 12 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]時會遇到異常。