gpt4 book ai didi

Android 应用程序崩溃 : Parsing JSON into ListView

转载 作者:行者123 更新时间:2023-12-03 16:14:24 24 4
gpt4 key购买 nike

我之前发布过,但仍然遇到同样的问题。我的 android 应用程序将加载此 Activity ,然后在它能够显示来自我的 JSON 对象的任何信息之前崩溃。我不知道为什么会这样。任何帮助,将不胜感激。

这是我不断崩溃的 Activity 的代码:

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 com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d11730 that was originally bound here
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d11730 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 com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d11730 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 android.os.BinderProxy@415528b0
02-26 19:17:53.411: ERROR/ActivityThread(856): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cdc7c0 that was originally bound here
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cdc7c0 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 com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cdc7c0 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 android.os.BinderProxy@415a58e8
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}

最佳答案

你是 NullPointerException异常(exception),因为您当前没有在 Array 中添加 10 项,您只在 eventArr 中添加了一项数组,但数组大小为 10 。

如果 ArrayList 中的项目是动态大小,建议使用 ArrayList 而不是 Array。将您的代码更改为:

声明 ArrayList 代替 Array :

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

和里面 onPostExecutedoInBackground AsyncTask 的方法将 json 项添加到 ArrayList,然后将 ArrayList 设置为 Adapter 的数据源:
  @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));

}

关于Android 应用程序崩溃 : Parsing JSON into ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15286428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com