2012-04-12 291 views
0

我想實現一個數組適配器,並且每次活動開始時我都會收到一個錯誤。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 
+1

後從日誌崩潰堆棧跟蹤。 – superfell 2012-04-12 15:51:39

+0

我現在添加了堆棧跟蹤。謝謝! – jimbob 2012-04-12 16:02:09

回答

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

+0

對不起,看起來像stackoverflow刪除我的標籤。我已經這樣做了......我用LogCat日誌和附加標籤更新了這篇文章。 – jimbob 2012-04-12 15:55:59

+0

好的更新Waqas謝謝:3! – jimbob 2012-04-12 16:01:51

+0

查看我的更新回答 – waqaslam 2012-04-12 16:26:01

0

好,唯一存在的是*可以爲null是你的json_participants_arr,所以必須有一些關於它的事情,ArrayAdapter構造uctor不喜歡。它在你的try/catch中被初始化。你有沒有研究過那個catch stacktrace來查看它是否因爲某種原因失敗了?

+0

也許我聲明JSON的類型錯誤..這是我從JSON供稿獲得:響應: { 「事件」:{\t .......一些其他的東西 「參與者」:[] }, ...一些其他的東西 } – jimbob 2012-04-12 16:34:33

0

你有一個空指針異常,從json_participants_arr爲空,可能是因爲拋出了你幾乎可以捕獲然後忽略的某個東西JSONException。 (不要這樣做),嘗試/抓住,看看你得到了什麼異常。

一旦你弄清楚了,你也會想回來修復它在UI線程上發出網絡請求(jParser.getJSONFromUrl線,看看loadersasync tasks)。取決於你使用的操作系統版本,這實際上可能是你的潛在問題。

0

使用它像這樣...

String[] arrParticipants = createArrayFromJSON(json_array_participants); 

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