2013-03-08 107 views
1

我早些時候發佈,但仍然有同樣的問題。我的android應用程序將加載此活動,然後崩潰,然後才能顯示來自我的JSON對象的任何信息。我不知道爲什麼。任何幫助,將不勝感激。Android應用崩潰:解析JSON到ListView

這裏是我的,保持活動代碼崩潰:

public class MainScreen extends ListActivity{ 
    JSONObject jsonObj= null; 

    String[] eventArr= new String[10]; 
    int i=0; 

    String[] urlArr = new String[10];//"http://accomplist.herokuapp.com/api/v1/sharedevent/1/?format=json"; 

    private static final String TAG_EVENT="event"; //A JSON object within the JSON object that will be returned by JSONParse() 
    private static final String TAG_DESCRIPTION="description"; //A JSON tag within the JSON object EVENT 
    private static String eventString="Yo"; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_screen); 
     ListView eventsList= getListView(); 
     for(int b=0; b<urlArr.length; b++) { 
      urlArr[b]= "http://accomplist.herokuapp.com/api/v1/sharedevent/"+(b+1)+"/?format=json"; 
     } 
     new JSONParse().execute(urlArr[0]); 
    } 
private class JSONParse extends AsyncTask<String, Void, String> { 
    HttpClient client=new DefaultHttpClient(); 


    @Override 
    protected String doInBackground(String... jsonurl) { 
//  StringBuilder url= new StringBuilder(String.valueOf(jsonurl)); 
     HttpUriRequest request= new HttpGet("http://accomplist.herokuapp.com/api/v1/sharedevent/2/?format=json"); 
     HttpResponse r= null; 
     try { 
      r = client.execute(request); 
     } catch (ClientProtocolException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
     } 
     int status= r.getStatusLine().getStatusCode(); 
     if (status==200){ 
      HttpEntity e=r.getEntity(); 
      String data= null; 
      try { 
       data = EntityUtils.toString(e); 
      } catch (IOException e1) { 
       e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
      } 
      try { 
       jsonObj = new JSONObject(data); 
      } catch (JSONException e1) { 
       e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
      } 
      try { 
       JSONObject eventJson= jsonObj.getJSONObject(TAG_EVENT); 
       eventString= eventJson.getString(TAG_DESCRIPTION); 
      } 
      catch (JSONException e1) { 
       eventString="Couldn't Parse Data"; 
      } 
      return eventString; 
     } 
     else{ 
      return eventString; 
     } 
    } 
    protected void onProgressUpdate() { 
     Toast loadingToast= Toast.makeText(getApplicationContext(), "Loading", Toast.LENGTH_LONG); 
     loadingToast.show(); 
    } 
    protected void onPostExecute(String result) { 
     eventString=result; 
     eventArr[i]=eventString; 
     i++; 
     MainScreen.this.setListAdapter(new ArrayAdapter<String>(MainScreen.this, 
       android.R.layout.simple_list_item_1,eventArr)); 

    } 
} 
} 

這裏是我的logcat錯誤:

com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]11730 that was originally bound here 
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]11730 that was originally bound here 
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969) 
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 
at android.app.ContextImpl.bindService(ContextImpl.java:1418) 
at android.app.ContextImpl.bindService(ContextImpl.java:1407) 
at android.content.ContextWrapper.bindService(ContextWrapper.java:473) 
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 
at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116) 
at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249) 
at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856) 
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 
at android.os.AsyncTask$2.call(AsyncTask.java:287) 
at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
at java.lang.Thread.run(Thread.java:856) 
02-26 19:17:53.381: ERROR/StrictMode(856): null 
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]11730 that was originally bound here 
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969) 
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 
at android.app.ContextImpl.bindService(ContextImpl.java:1418) 
at android.app.ContextImpl.bindService(ContextImpl.java:1407) 
at android.content.ContextWrapper.bindService(ContextWrapper.java:473) 
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 
at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116) 
at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249) 
at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856) 
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 
at android.os.AsyncTask$2.call(AsyncTask.java:287) 
at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
at java.lang.Thread.run(Thread.java:856) 
02-26 19:17:53.381: WARN/ActivityManager(324): Unbind failed: could not find connection for [email protected] 
02-26 19:17:53.411: ERROR/ActivityThread(856): Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]dc7c0 that was originally bound here 
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]dc7c0 that was originally bound here 
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969) 
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 
at android.app.ContextImpl.bindService(ContextImpl.java:1418) 
at android.app.ContextImpl.bindService(ContextImpl.java:1407) 
at android.content.ContextWrapper.bindService(ContextWrapper.java:473) 
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 
at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 
at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850) 
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 
at android.os.AsyncTask$2.call(AsyncTask.java:287) 
at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
at java.lang.Thread.run(Thread.java:856) 
02-26 19:17:53.491: DEBUG/dalvikvm(7047): Trying to load lib libjavacore.so 0x0 
02-26 19:17:53.511: ERROR/StrictMode(856): null 
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]dc7c0 that was originally bound here 
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969) 
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 
at android.app.ContextImpl.bindService(ContextImpl.java:1418) 
at android.app.ContextImpl.bindService(ContextImpl.java:1407) 
at android.content.ContextWrapper.bindService(ContextWrapper.java:473) 
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 
at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 
at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850) 
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 
at android.os.AsyncTask$2.call(AsyncTask.java:287) 
at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
at java.lang.Thread.run(Thread.java:856) 
02-26 19:17:53.531: WARN/ActivityManager(324): Unbind failed: could not find connection for [email protected] 
02-26 19:17:53.531: DEBUG/dalvikvm(7047): Added shared lib libjavacore.so 0x0 
02-26 19:17:53.671: DEBUG/dalvikvm(7047): Trying to load lib libnativehelper.so 0x0 
02-26 19:17:53.671: DEBUG/dalvikvm(7047): Added shared lib libnativehelper.so 0x0 
02-26 19:17:53.771: DEBUG/dalvikvm(856): GC_CONCURRENT freed 464K, 19% free 2624K/3216K, paused 21ms+13ms, total 425ms 
02-26 19:17:55.471: DEBUG/AndroidRuntime(7047): Calling main entry com.android.commands.am.Am 
02-26 19:17:55.491: DEBUG/dalvikvm(7047): Note: class Landroid/app/ActivityManagerNative; has 157 unimplemented (abstract) methods 
02-26 19:17:55.561: INFO/ActivityManager(324): START u0 {flg=0x10000000 cmp=com.example.Accomplist/.MyActivity} from pid 7047 
02-26 19:17:55.621: WARN/WindowManager(324): Failure taking screenshot for (246x437) to layer 21030 
02-26 19:17:55.711: DEBUG/AndroidRuntime(7047): Shutting down VM 
02-26 19:17:55.751: DEBUG/dalvikvm(7060): Not late-enabling CheckJNI (already on) 
02-26 19:17:55.751: DEBUG/dalvikvm(7047): GC_CONCURRENT freed 91K, 16% free 491K/584K, paused 2ms+2ms, total 43ms 
02-26 19:17:55.776: DEBUG/jdwp(7047): Got wake-up signal, bailing out of select 
02-26 19:17:55.776: DEBUG/dalvikvm(7047): Debugger has detached; object registry had 1 entries 
02-26 19:17:55.832: INFO/ActivityManager(324): Start proc com.example.Accomplist for activity com.example.Accomplist/.MyActivity: pid=7060 uid=10048 gids={50048, 3003, 1028} 
02-26 19:17:55.845: INFO/Choreographer(652): Skipped 49 frames! The application may be doing too much work on its main thread. 
02-26 19:17:56.642: ERROR/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property 
02-26 19:17:57.032: ERROR/Trace(7060): error opening trace file: No such file or directory (2) 
02-26 19:17:59.312: ERROR/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property 
02-26 19:17:59.552: DEBUG/gralloc_goldfish(7060): Emulator without GPU emulation detected. 
02-26 19:17:59.742: DEBUG/dalvikvm(528): GC_EXPLICIT freed 159K, 5% free 5791K/6072K, paused 25ms+35ms, total 331ms 
02-26 19:17:59.891: INFO/ActivityManager(324): Displayed com.example.Accomplist/.MyActivity: +4s204ms (total +35s239ms) 
02-26 19:18:00.292: INFO/Choreographer(1137): Skipped 116 frames! The application may be doing too much work on its main thread. 
02-26 19:18:01.021: INFO/Choreographer(652): Skipped 62 frames! The application may be doing too much work on its main thread. 
02-26 19:18:04.122: INFO/ActivityManager(324): START u0 {act=com.example.Accomplist.MAINSCREEN cmp=com.example.Accomplist/.MainScreen} from pid 7060 
02-26 19:18:04.142: WARN/WindowManager(324): Failure taking screenshot for (246x437) to layer 21035 
02-26 19:18:05.152: DEBUG/dalvikvm(7060): GC_FOR_ALLOC freed 69K, 7% free 2606K/2796K, paused 295ms, total 328ms 
02-26 19:18:05.182: INFO/dalvikvm-heap(7060): Grow heap (frag case) to 3.267MB for 635812-byte allocation 
02-26 19:18:05.432: DEBUG/dalvikvm(7060): GC_FOR_ALLOC freed 5K, 6% free 3222K/3420K, paused 249ms, total 249ms 
02-26 19:18:05.822: DEBUG/dalvikvm(7060): GC_CONCURRENT freed <1K, 6% free 3234K/3420K, paused 15ms+74ms, total 382ms 
02-26 19:18:06.292: ERROR/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property 
02-26 19:18:06.331: INFO/Choreographer(7060): Skipped 40 frames! The application may be doing too much work on its main thread. 
02-26 19:18:06.812: INFO/ActivityManager(324): Displayed com.example.Accomplist/.MainScreen: +2s647ms 
02-26 19:18:09.561: DEBUG/AndroidRuntime(7060): Shutting down VM 
02-26 19:18:09.571: WARN/dalvikvm(7060): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
02-26 19:18:09.661: ERROR/AndroidRuntime(7060): FATAL EXCEPTION: main 
java.lang.NullPointerException 
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394) 
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 
at android.widget.AbsListView.obtainView(AbsListView.java:2143) 
at android.widget.ListView.measureHeightOfChildren(ListView.java:1246) 
at android.widget.ListView.onMeasure(ListView.java:1158) 
at android.view.View.measure(View.java:15518) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825) 
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
at android.view.View.measure(View.java:15518) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at android.view.View.measure(View.java:15518) 
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847) 
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
at android.view.View.measure(View.java:15518) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176) 
at android.view.View.measure(View.java:15518) 
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874) 
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089) 
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265) 
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) 
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
at android.view.Choreographer.doCallbacks(Choreographer.java:562) 
at android.view.Choreographer.doFrame(Choreographer.java:532) 
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
at android.os.Handler.handleCallback(Handler.java:725) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5041) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
at dalvik.system.NativeStart.main(Native Method) 
02-26 19:18:09.771: WARN/ActivityManager(324): Force finishing activity com.example.Accomplist/.MainScreen 
02-26 19:18:09.922: WARN/WindowManager(324): Failure taking screenshot for (246x437) to layer 21040 
02-26 19:18:10.271: INFO/Choreographer(324): Skipped 45 frames! The application may be doing too much work on its main thread. 
02-26 19:18:10.281: ERROR/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property 
02-26 19:18:10.499: WARN/ActivityManager(324): Activity pause timeout for ActivityRecord{40da7188 u0 com.example.Accomplist/.MainScreen} 
02-26 19:18:19.976: WARN/ActivityManager(324): Launch timeout has expired, giving up wake lock! 
02-26 19:18:20.776: WARN/ActivityManager(324): Activity idle timeout for ActivityRecord{40d319a 

0 u0 com.example.Accomplist/.MyActivity} 

回答

0

NullPointerException例外,因爲您不添加內部陣列10個項目目前ü在eventArr陣列中只添加一個項目,但數組大小爲10。

如果項目是ArrayList中的動態大小,建議使用ArrayList而不是Array。改變你的代碼:

聲明的ArrayList代替陣列:

ArrayList<String> eventArr=new ArrayList<String>(); 

和內部onPostExecutedoInBackground的AsyncTask的方法添加JSON項目ArrayList中,然後設置的ArrayList作爲datasoucre的適配器:

@Override 
protected void onPostExecute(String result) { 

     eventString=result; 

     eventArr.add(result); //<< add item to ArrayList 

     MainScreen.this.setListAdapter(new ArrayAdapter<String>(MainScreen.this, 
       android.R.layout.simple_list_item_1,eventArr)); 

    } 
+0

感謝這,這實際上證明是解決方案!你的幫助是非常感謝:) – sourdesi 2013-03-08 06:35:49

+0

@sourdesi:如果這有助於你解決目前的問題,最歡迎的朋友可以將它作爲答案。謝謝 – 2013-03-08 06:45:16