2016-10-16 62 views
0

我試圖在AlertDialog中選擇一個選項,但它顯示錯誤。下面是錯誤:在Firebase數據庫點擊AlertDialog設置值時出錯

10-17 00:54:44.765 25600-25600/com.example.jingwen.bluetoothlowenergy E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.jingwen.bluetoothlowenergy, PID: 25600 
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
at java.util.ArrayList.get(ArrayList.java:308) 
at com.example.jingwen.bluetoothlowenergy.MainActivity$1$1.onClick(MainActivity.java:325) 
at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:959) 
at android.widget.AdapterView.performItemClick(AdapterView.java:300) 
at android.widget.AbsListView.performItemClick(AbsListView.java:1143) 
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3063) 
at android.widget.AbsListView$3.run(AbsListView.java:3881) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5237) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707) 

這是發生錯誤的代碼,具體的setValue部分火力地堡數據庫:

private void alertdialog() 
 
    { 
 
     final CharSequence peers[] = new CharSequence[] {"Home", "School", "Children"}; 
 
     stopScan(); 
 
     final String uid = firebaseAuth.getCurrentUser().getUid(); 
 
     if(alert11!=null && alert11.isShowing()) return; 
 
     AlertDialog.Builder builder1 = new AlertDialog.Builder(this); 
 
     builder1.setMessage("Add this device to peer list?"); 
 
     builder1.setCancelable(true); 
 

 
     builder1.setNegativeButton(
 
       "Yes", 
 
       new DialogInterface.OnClickListener() { 
 
        public void onClick(DialogInterface dialog, int id) { 
 
         AlertDialog.Builder pbuilder = new AlertDialog.Builder(MainActivity.this); 
 
         pbuilder.setTitle("Set peer as:"); 
 
         pbuilder.setCancelable(false); 
 
         pbuilder.setItems(peers, new DialogInterface.OnClickListener() { 
 
          @Override 
 
          public void onClick(DialogInterface dialog, int which) { 
 
           // the user clicked on option[which] 
 
           if(which == 0) { 
 
            databaseRef.child("users").child(uid).child("Peer list").child(mBTDevicesArrayList.get(post).getAddress()).setValue("Home"); 
 
            Toast.makeText(MainActivity.this,"Set peer as 'Home'",Toast.LENGTH_SHORT).show(); 
 
            Toast.makeText(MainActivity.this,"Peer list updated!",Toast.LENGTH_SHORT).show(); 
 
           } 
 
           if(which == 1) { 
 
            databaseRef.child("users").child(uid).child("Peer list").child(mBTDevicesArrayList.get(post).getAddress()).setValue("School"); 
 
            Toast.makeText(MainActivity.this,"Set peer as 'School'",Toast.LENGTH_SHORT).show(); 
 
            Toast.makeText(MainActivity.this,"Peer list updated!",Toast.LENGTH_SHORT).show(); 
 
           } 
 
           if(which == 2) { 
 
            databaseRef.child("users").child(uid).child("Peer list").child(mBTDevicesArrayList.get(post).getAddress()).setValue("Children"); 
 
            Toast.makeText(MainActivity.this,"Set peer as 'Children'",Toast.LENGTH_SHORT).show(); 
 
            Toast.makeText(MainActivity.this,"Peer list updated!",Toast.LENGTH_SHORT).show(); 
 
           } 
 
          } 
 
         }); 
 
         pbuilder.show(); 
 

 
        } 
 
       });

一定要告訴我,如果有代碼的多個部分需要顯示。

回答

0

這可能與Firebase無關。如我所知,mBTDevicesArrayList(定義和賦值未在上圖中顯示)是一個大小爲零的ArrayList,並且將值1傳遞給其get方法,如錯誤消息「java」所示。 lang.IndexOutOfBoundsException:索引1無效,大小爲0「。堆棧跟蹤中的行號將幫助您找出正在顯示的三個實例中的哪一個。

+0

謝謝。正如你所說,我的ArrayList有問題。 – jwen

相關問題