1

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

クラスはアテンドと呼ばれます。配列に値が 1 つある場合、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];

                 }
4

2 に答える 2

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

ループに入るたびに配列を再作成し、以前の値を消去しています。

于 2012-04-16T19:05:42.447 に答える
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 にない可能性はありますか? ヌルポインタ例外について説明します。

于 2012-04-16T19:07:06.347 に答える