阵列中有多个项目时,ArrayAdapter错误

时间:2012-04-16 18:58:46

标签: android listview android-arrayadapter create-view

当我得到一个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);

该课程称为Attend。当数组中有一个值时,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];

                     }

FIXED:我每次都在创建新的数组,所以它仍然存储了值,但它导致了崩溃...继承了更新的修复程序。干杯们:

   //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];

                 }

2 个答案:

答案 0 :(得分:4)

这应该在你的循环之外:

                         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()];

每次进入循环时都会重新创建数组,删除之前的任何值。

答案 1 :(得分:1)

我猜这个错误就在这里:

str_amparticipant_firstname[i] = json_array_amparticipants.getJSONObject(i).getString("first_name"); str_amparticipant_lastname[i] = json_array_amparticipants.getJSONObject(i).getString("last_name");

这些密钥是否可能不在JSONObject中?它将解释空指针异常。