当我得到一个在 ListView 中包含多个值的数组时出现错误...它崩溃了...
str_amparticipant_firstname 是一个字符串值数组。str_pmparticipant_firstname 也是一个字符串值数组。
adapteram = new ArrayAdapter<String>(Attend.this, android.R.layout.simple_list_item_1, str_amparticipant_firstname);
adapterpm = new ArrayAdapter<String>(Attend.this, android.R.layout.simple_list_item_1, str_pmparticipant_firstname);
amattendees.setAdapter(adapteram);
pmattendees.setAdapter(adapterpm);
该课程称为参加。当数组中有一个值时,ListViews 会显示数组中的值。如果有多个值,应用程序就会崩溃。我已经检查了数组是否被正确填充,并且是正确的。希望错误日志有答案。我得到的错误日志如下:
04-16 19:28:57.664: E/AndroidRuntime(5807): FATAL EXCEPTION: main
04-16 19:28:57.664: E/AndroidRuntime(5807): java.lang.NullPointerException
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.ListView.measureHeightOfChildren(ListView.java:1307)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.ListView.onMeasure(ListView.java:1127)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.view.View.measure(View.java:8510)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3202)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:701)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.LinearLayout.onMeasure(LinearLayout.java:311)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.view.View.measure(View.java:8510)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3202)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.view.View.measure(View.java:8510)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3202)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.view.View.measure(View.java:8510)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.view.View.measure(View.java:8510)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3202)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.view.View.measure(View.java:8510)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.view.ViewRoot.performTraversals(ViewRoot.java:871)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.view.ViewRoot.handleMessage(ViewRoot.java:1921)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.os.Looper.loop(Looper.java:143)
04-16 19:28:57.664: E/AndroidRuntime(5807): at android.app.ActivityThread.main(ActivityThread.java:4196)
04-16 19:28:57.664: E/AndroidRuntime(5807): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 19:28:57.664: E/AndroidRuntime(5807): at java.lang.reflect.Method.invoke(Method.java:507)
04-16 19:28:57.664: E/AndroidRuntime(5807): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-16 19:28:57.664: E/AndroidRuntime(5807): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-16 19:28:57.664: E/AndroidRuntime(5807): at dalvik.system.NativeStart.main(Native Method)
再次,仅当数组中有多个值时才会出现错误。我试图找到这个问题的答案,但我找不到有关该错误的任何信息。请帮忙!谢谢你!
更新:这是填充和创建数组的方式:
for(int i = 0; i < json_array_amparticipants.length(); i++){
//store all the amparticipants
str_amparticipant_email = new String[json_array_amparticipants.length()];
str_amparticipant_firstname = new String[json_array_amparticipants.length()];
str_amparticipant_lastname = new String[json_array_amparticipants.length()];
str_amparticipant_attendingam = new String[json_array_amparticipants.length()];
str_amparticipant_attendingpm = new String[json_array_amparticipants.length()];
//full_name_am = new String[json_array_amparticipants.length()];
str_amparticipant_email[i] = json_array_amparticipants.getJSONObject(i).getString("email");
str_amparticipant_firstname[i] = json_array_amparticipants.getJSONObject(i).getString("first_name");
str_amparticipant_lastname[i] = json_array_amparticipants.getJSONObject(i).getString("last_name");
str_amparticipant_attendingam[i] = json_array_amparticipants.getJSONObject(i).getString("attending_am");
str_amparticipant_attendingpm[i] = json_array_amparticipants.getJSONObject(i).getString("attending_pm");
//full_name_am[i] = str_amparticipant_firstname[i] + str_amparticipant_lastname[i];
}
修复:我每次都在创建新数组,所以它仍然存储值,但它会导致崩溃......这是更新的修复。伙计们干杯:
//store all the amparticipants
str_amparticipant_email = new String[json_array_amparticipants.length()];
str_amparticipant_firstname = new String[json_array_amparticipants.length()];
str_amparticipant_lastname = new String[json_array_amparticipants.length()];
str_amparticipant_attendingam = new String[json_array_amparticipants.length()];
str_amparticipant_attendingpm = new String[json_array_amparticipants.length()];
for(int i = 0; i < json_array_amparticipants.length(); i++){
//full_name_am = new String[json_array_amparticipants.length()];
str_amparticipant_email[i] = json_array_amparticipants.getJSONObject(i).getString("email");
str_amparticipant_firstname[i] = json_array_amparticipants.getJSONObject(i).getString("first_name");
str_amparticipant_lastname[i] = json_array_amparticipants.getJSONObject(i).getString("last_name");
str_amparticipant_attendingam[i] = json_array_amparticipants.getJSONObject(i).getString("attending_am");
str_amparticipant_attendingpm[i] = json_array_amparticipants.getJSONObject(i).getString("attending_pm");
//full_name_am[i] = str_amparticipant_firstname[i] + str_amparticipant_lastname[i];
}