Я попытался выбрать параметр в 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 для базы данных Firebase:
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();
}
});
Скажите мне, нужно ли показывать некоторые части кодов.
javascript,java,firebase,firebase-realtime-database,