我想實現一個數組適配器,並且每次活動開始時我都會收到一個錯誤。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
後從日誌崩潰堆棧跟蹤。 – superfell 2012-04-12 15:51:39
我現在添加了堆棧跟蹤。謝謝! – jimbob 2012-04-12 16:02:09