我尝试在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)
这是发生错误的代码,特别是Firebase数据库的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 :(得分:0)
这可能与Firebase无关。
据我所知,mBTDevicesArrayList(上面未显示的定义和赋值)是一个大小为零的ArrayList,并且您将值1传递给其get方法,如错误消息“java.lang所示” .IndexOutOfBoundsException:索引1无效,大小为0“。堆栈跟踪中的行号将帮助您确定要显示的三个实例中的哪一个。