ArrayAdapter导致崩溃

时间:2012-04-12 15:49:56

标签: android listview android-arrayadapter

我正在尝试实现一个数组适配器,每次活动开始时我都会收到错误。

这是我的班级,我注意到导致崩溃的线路。

public class Attend extends Activity{

ListView amattendees;
ListView pmattendees;
String attendingAm[];
String attendingPm[];
JSONParser jParser = new JSONParser();
 JSONObject json_event;
 String json_name = null;
 String json_badge_id = null;
 String json_venue_id = null;
 String json_event_id = null;
 String json_id = null;
 String json_date = null;
 String json_amLesson = null;
 String json_pmLesson = null;
 String json_youtubeid = null;
 String json_lessonHTML = null;
 String json_active = null;
 String json_created = null;
 String json_attending = null;
 String json_status = null;
 String fragmented = null;
 String json_title = null;
 String json_event_description = null;

 String[] participants = null;
 String setAttending_response = "Attend";
 String scannedBar = "default";
 SharedPreferences settings;
 String json_videopath;
 String str_accesstoken;
 Context context;
 VideoView youtubeVideo;
 String sAttending = "false";
 String home_venue_long = null;
 String home_venue_lat = null;
 private static final int REQUEST_BARCODE = 0;
 double my_longitude;
 double my_latitude;
 String json_venue_distance;
 String json_nearest_venue;
 String json_nearest_venue_logo;
 String json_nearest_venue_long;
 String json_nearest_venue_lat;
 String  json_participants_arr[];


 public String[] createArrayFromJSON(JSONArray json_array_participants){
      String json_participants[] = new String[json_array_participants.length()];
      json_participants_arr = new String[json_array_participants.length()];
 for(int i = 0; i < json_participants.length; i++){
     try{
         json_participants[i] = json_array_participants.getString(i);   
         json_participants_arr[i] = json_array_participants.getString(i);  
        }
        catch(JSONException e){
         e.getStackTrace();
        }
  }
 return json_participants;
 }



 public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.attend);            

        amattendees = (ListView)findViewById(R.id.amattendees);
        pmattendees = (ListView)findViewById(R.id.pmattendees);



         try{
                //get the JSON values from the URL.
                JSONObject json = jParser.getJSONFromUrl("http://ajsonfeed.com?hi=23849348934");


               json_event = json.getJSONObject("event");
               json_name = json_event.getString("name");
               json_status = json.getString("status");
               json_badge_id =  json_event.getString("badge_id");
               json_venue_id = json_event.getString("venue_id");
               json_event_id = json_event.getString("event_id");
               json_id = json_event.getString("id");
               json_date = json_event.getString("date");
               json_amLesson = json_event.getString("amLesson");
               json_pmLesson = json_event.getString("pmLesson");
               json_youtubeid= json_event.getString("youtubeId");
               json_lessonHTML = json_event.getString("lessonHTML");
               json_active = json_event.getString("active");
               json_created = json_event.getString("created");
               json_attending = json_event.getString("attending");
               fragmented = json_event.getString("fragmented");
               JSONArray json_array_participants = json_event.getJSONArray("participants");   

               createArrayFromJSON(json_array_participants);


                }
                catch(JSONException e){
                    e.getStackTrace();
                }

这条线崩溃了。

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,android.R.id.text1, json_participants_arr);

 }






}

来自logcat的错误日志:

04-12 16:58:32.683: E/AndroidRuntime(7920): FATAL EXCEPTION: main
04-12 16:58:32.683: E/AndroidRuntime(7920): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.jujitsu.app.com/org.jujitsu.app.com.Attend}: java.lang.NullPointerException
04-12 16:58:32.683: E/AndroidRuntime(7920):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at android.os.Looper.loop(Looper.java:143)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at android.app.ActivityThread.main(ActivityThread.java:4196)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at java.lang.reflect.Method.invokeNative(Native Method)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at java.lang.reflect.Method.invoke(Method.java:507)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at dalvik.system.NativeStart.main(Native Method)
04-12 16:58:32.683: E/AndroidRuntime(7920): Caused by: java.lang.NullPointerException
04-12 16:58:32.683: E/AndroidRuntime(7920):     at java.util.Arrays$ArrayList.<init>(Arrays.java:47)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at java.util.Arrays.asList(Arrays.java:169)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:138)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at org.jujitsu.app.com.Attend.onCreate(Attend.java:124)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
04-12 16:58:32.683: E/AndroidRuntime(7920):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
04-12 16:58:32.683: E/AndroidRuntime(7920):     ... 11 more

4 个答案:

答案 0 :(得分:1)

这样做:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1,
                      createArrayFromJSON(json_array_participants));

并在json的try

中移动它

答案 1 :(得分:0)

那么,*可以为null的唯一东西就是你的json_participants_arr,所以ArrayAdapter构造函数不喜欢它。它在你的try / catch中初始化。您是否考虑过捕获堆栈跟踪以查看它是否由于某种原因而失败?

答案 2 :(得分:0)

你有一个来自json_participants_arr的空指针异常为空,可能是因为某些东西扔了JSONException你几乎抓住然后忽略了。 (不要这样做),尝试/赶上,看看你得到了什么例外。

一旦你理解了这一点,你也会想要回来修复它在UI线程上发出网络请求(jParser.getJSONFromUrl行,看看loadersasync tasks) 。根据您使用的操作系统版本,这实际上可能是您的潜在问题。

答案 3 :(得分:0)

像这样使用它......

String[] arrParticipants = createArrayFromJSON(json_array_participants);

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,android.R.id.text1, arrParticipants );