我得到了这个奇怪的错误。我无法弄清楚为什么会发生这种情况。
这是问题
如果我将此代码放在firebaseproblem.onDataChange
中,我收到了错误。
String pl = "Ali";
items[0] = pl;
如果我放在其他任何地方它完美地工作......它是items[0]
用于调试目的
错误记录
07-20 12:16:06.605 2720-2720/azcrew.eatapp E/UncaughtException: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at azcrew.eatapp.halfdone.listComp$2.onDataChange(listComp.java:98)
at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
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:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
07-20 12:16:06.845 2720-2720/azcrew.eatapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: azcrew.eatapp, PID: 2720
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at azcrew.eatapp.halfdone.listComp$2.onDataChange(listComp.java:98)
at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
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:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
这是完整的代码
public class listComp extends AppCompatActivity {
ListView listView;
String[] id;
String[] items = new String[20];
int index = -1;
static String pl;
listMVP l = new listMVP();
private static final String TAG = "test";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_comp);
listView = (ListView) findViewById(R.id.listcomp);
Bundle b = this.getIntent().getExtras();
id = b.getStringArray("id");
firebaseproblem1();
removingnull();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item1, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int d, long l) {
String a = ((TextView) view).getText().toString();
for(int ind = 0; d<items.length;ind++){
if(items[ind].equals(a)){
index = ind;
break;
}
}
//intentIn();
}
});
}
private void firebaseproblem1() {
for(int i=0; i<id.length;i++){
if(id[i]!= null){
DatabaseReference ref5 = FirebaseDatabase.getInstance().getReference();
DatabaseReference q = ref5.child("Restoran").child(id[i]).child("id");
l.setI(i);
final int finalI = i;
q.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
pl = "Ali";
items[0] = pl;
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
}
private void removingnull() {
List<String> list = new ArrayList<String>();
for(String s: items){
if(s!= null){
list.add(s);
}
}
items = list.toArray(new String[list.size()]);
}
private void intentIn() {
Bundle c = new Bundle();
c.putString("resId",id[index]);
Intent i1 = new Intent(this, ListMain.class);
i1.putExtras(c);
startActivity(i1);
}
}
答案 0 :(得分:1)
您的onDataChanged
功能可能会在将来的任何时间被调用。
与此同时,您的代码调用了removingnull
函数,该函数已将items
数组重置为空数组。
如果我放在其他任何地方它完美无缺
这是因为它在removingnull
函数之前同步执行,因此items
数组仍然是String[20]
您只需记录一些消息就可以说服自己执行代码的顺序。
建议修复:
忘记数组,让项目为List<String>
,你不会遇到这类问题。