java.lang.ArrayIndexOutOfBounds尝试从 Firebase 显示列表视图时出现异常错误



我正在构建一个应用程序,用户可以在其中将数据存储到 Firebase 数据库中,并可以通过日志片段上的列表视图访问它,因此一开始我将数据存储在输入活动的哈希映射中:

String BloodLevels = (bgET.getText().toString());
String Carbohydrates = (carbET.getText().toString());
String InsulinTaken = (insulinET.getText().toString());
String DateandTime = DateFormat.getDateTimeInstance().format(new Date());
HashMap<String, String> dataMap = new HashMap<String, String>();
dataMap.put("DateandTime", DateandTime);
dataMap.put("BloodLevels", BloodLevels);
dataMap.put("Carbohydrates", Carbohydrates);
dataMap.put("Insulin", InsulinTaken);
dataRef.push().setValue(dataMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
progress.dismiss();
Toast.makeText(InputActivity.this, "Your Data Has Been Saved", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(InputActivity.this, MainActivity.class);
startActivity(intent);
finish();

}else{
progress.dismiss();
Toast.makeText(InputActivity.this, "Error: Your data could not be saved", Toast.LENGTH_SHORT).show();
}

然后,我尝试在日志片段中的列表视图中调用数据:

public void onActivityCreated(Bundle savedInstanceState) {
getActivity().setTitle("Logbook");
super.onActivityCreated(savedInstanceState);
databaseQuery = FirebaseDatabase.getInstance().getReference().child("Entries");
databaseQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
HashMap<String, String> map = (HashMap<String, String>) dataSnapshot.getValue();
ListView listView = (ListView) getActivity().findViewById(R.id.entriesList);
List<HashMap<String,String>> listitems = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(getActivity(),listitems,R.layout.list_item_log,
new String[]{"First Line", "Second Line, Third Line, Fourth Line"},
new int[]{R.id.text1, R.id.text2, R.id.text3, R.id.text4});
Iterator it = map.entrySet().iterator();
while (it.hasNext()){
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry)it.next();
resultsMap.put("First Line", pair.getKey().toString());
resultsMap.put("Second Line", pair.getValue().toString());
resultsMap.put("Third Line", pair.getKey().toString());
resultsMap.put("Fourth Line", pair.getValue().toString());
resultsMap.put("Fifth Line", pair.getKey().toString());
resultsMap.put("Sixth Line", pair.getValue().toString());
resultsMap.put("Seventh Line", pair.getKey().toString());
resultsMap.put("Eighth Line", pair.getValue().toString());
listitems.add(resultsMap);
}
listView.setAdapter(adapter);
}

这是我在列表视图上使用的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text1"
android:textColor="@color/textDark"
android:textSize="14dp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text2"
android:textColor="@color/textDark"
android:textSize="14dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text3"
android:textColor="@color/textDark"
android:textSize="14dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text4"
android:textColor="@color/textDark"
android:textSize="14dp"/>
</LinearLayout>

但是当我启动应用程序时,我收到此错误并且应用程序崩溃:

E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.kenjasim.glucosehelper, PID: 10154   java.lang.ArrayIndexOutOfBoundsException: length=2; index=2

所以我不知道如何解决这个问题,并希望我能得到任何帮助。谢谢

部分错误消息没有透露太多,但我希望这是您SimpleAdapter构造函数中的错误。

SimpleAdapter adapter = new SimpleAdapter(getActivity(),listitems,R.layout.list_item_log,
new String[]{"First Line", "Second Line, Third Line, Fourth Line"},
new int[]{R.id.text1, R.id.text2, R.id.text3, R.id.text4});

看起来意图是拥有一个包含 4 个字符串的数组,但通过引号的排列,"Second Line, Third Line, Fourth Line"是一个字符串,这意味着数组中只有两个字符串。

尝试:

SimpleAdapter adapter = new SimpleAdapter(getActivity(),listitems,R.layout.list_item_log,
new String[]{"First Line", "Second Line", "Third Line", "Fourth Line"},
new int[]{R.id.text1, R.id.text2, R.id.text3, R.id.text4});

最新更新