0

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

这是我的课,我已经注意到导致崩溃的行。

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

4 回答 4

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块中

于 2012-04-12T15:52:54.250 回答
0

好吧,唯一可以为 null 的就是你的 json_participants_arr,所以一定有一些 ArrayAdapter 构造函数不喜欢的地方。它在您的 try/catch 中初始化。您是否查看了该 catch 堆栈跟踪以查看它是否由于某种原因而失败?

于 2012-04-12T16:09:23.583 回答
0

你有一个空指针异常json_participants_arr,可能是因为JSONException你几乎抓住然后忽略了一些东西。(不要那样做),把 try/catchs 拿出来看看你得到了什么异常。

一旦你把它理顺了,你还想回来并通过在 UI 线程上发出网络请求来修复它(jParser.getJSONFromUrl 行,查看加载程序异步任务)。根据您使用的操作系统版本,这实际上可能是您的潜在问题。

于 2012-04-12T16:12:19.050 回答
0

像这样使用它...

String[] arrParticipants = createArrayFromJSON(json_array_participants);

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,android.R.id.text1, arrParticipants );
于 2012-04-12T16:18:19.043 回答