我使用AsyncTask
获取数据,然后将其加载到ListView
。我的代码是:
AsyncTaskTwitterFeeds asyncTaskTwitterFeeds = new AsyncTaskTwitterFeeds(TwitterFeedsActivity.this);
asyncTaskTwitterFeeds.execute("");
loadList();
在上面的代码中,执行AsyncTask
后,流程进入loadlist()
方法,程序崩溃。我发现原因是Adapter
中使用的数组仍在填充。我想知道我执行AsyncTask
后如何执行,只有当它完成后才能进入loadlist()
方法。这是LogCat
错误:
02-25 18:50:25.879: E/AndroidRuntime(19851): FATAL EXCEPTION: main
02-25 18:50:25.879: E/AndroidRuntime(19851): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cubix.twitterfeed/com.cubix.twitterfeed.TwitterFeedsActivity}: java.lang.NullPointerException
02-25 18:50:25.879: E/AndroidRuntime(19851): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
02-25 18:50:25.879: E/AndroidRuntime(19851): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
02-25 18:50:25.879: E/AndroidRuntime(19851): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
02-25 18:50:25.879: E/AndroidRuntime(19851): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
02-25 18:50:25.879: E/AndroidRuntime(19851): at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 18:50:25.879: E/AndroidRuntime(19851): at android.os.Looper.loop(Looper.java:143)
02-25 18:50:25.879: E/AndroidRuntime(19851): at android.app.ActivityThread.main(ActivityThread.java:4196)
02-25 18:50:25.879: E/AndroidRuntime(19851): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 18:50:25.879: E/AndroidRuntime(19851): at java.lang.reflect.Method.invoke(Method.java:507)
02-25 18:50:25.879: E/AndroidRuntime(19851): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-25 18:50:25.879: E/AndroidRuntime(19851): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-25 18:50:25.879: E/AndroidRuntime(19851): at dalvik.system.NativeStart.main(Native Method)
02-25 18:50:25.879: E/AndroidRuntime(19851): Caused by: java.lang.NullPointerException
02-25 18:50:25.879: E/AndroidRuntime(19851): at java.util.Arrays$ArrayList.<init>(Arrays.java:47)
02-25 18:50:25.879: E/AndroidRuntime(19851): at java.util.Arrays.asList(Arrays.java:169)
02-25 18:50:25.879: E/AndroidRuntime(19851): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:125)
02-25 18:50:25.879: E/AndroidRuntime(19851): at com.cubix.twitterfeed.TwitterAdapter.<init>(TwitterAdapter.java:17)
02-25 18:50:25.879: E/AndroidRuntime(19851): at com.cubix.twitterfeed.TwitterFeedsActivity.loadList(TwitterFeedsActivity.java:48)
02-25 18:50:25.879: E/AndroidRuntime(19851): at com.cubix.twitterfeed.TwitterFeedsActivity.onCreate(TwitterFeedsActivity.java:30)
02-25 18:50:25.879: E/AndroidRuntime(19851): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
02-25 18:50:25.879: E/AndroidRuntime(19851): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
02-25 18:50:25.879: E/AndroidRuntime(19851): ... 11 more
答案 0 :(得分:5)
AsyncTask
有一个方法onPostExecute()
,在AsyncTask
完成其在方法doInBackground()
中的工作之后,在主UI线程上执行。如果调用loadList()
,则可以使用该方法填充列表,以使用AsyncTask
的结果填充列表。