当我listview.setAdapter(adapter);
从 AsyncTask 的onPostExecute()
方法调用时出现此错误:
07-12 19:29:38.147: E/AndroidRuntime(8478): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
*注意:此崩溃仅发生在 API 级别 10 中,但不会发生在最新的 API 中(在 API 17 上运行良好)。
这是 onPostExecute():
@Override
protected void onPostExecute(String[] result) {
super.onPostExecute(result);
// TODO: check this.exception
er=result;//er is a global variable
gff=true;//gff is a global variable
asynchelp(er);//function in the UI thread
myPd_ring.dismiss();//dismissing a spinning ring
}
asynchelp()
在下面:
public void asynchelp(String[] er){
String iamoutofnames[]=null;
for(int q = 0; q <er.length; q++){
HashMap<String, String> map = new HashMap<String, String>();
iamoutofnames=null;
int len;
if(er[q].contains("sandy")){
//calculations
map.put("col_1", iamoutofnames[len]);
}else{
//calculations
map.put("col_1", iamoutofnames[1]);
}
fillMaps.add(map);
lv.setOnItemClickListener(onListClick);
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);//CRASHES ON THIS LINE**************
}
代码有什么问题?
编辑:堆栈跟踪:
07-12 19:29:37.996: W/System.err(8478): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-12 19:29:37.996: W/System.err(8478): at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
07-12 19:29:38.006: W/System.err(8478): at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
07-12 19:29:38.006: W/System.err(8478): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
07-12 19:29:38.006: W/System.err(8478): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
07-12 19:29:38.006: W/System.err(8478): at android.view.View.invalidate(View.java:5279)
07-12 19:29:38.006: W/System.err(8478): at android.widget.AbsListView.resetList(AbsListView.java:1150)
07-12 19:29:38.006: W/System.err(8478): at android.widget.ListView.resetList(ListView.java:511)
07-12 19:29:38.006: W/System.err(8478): at android.widget.ListView.setAdapter(ListView.java:440)
07-12 19:29:38.006: W/System.err(8478): at com.example.kiva.ListViewA.asynchelp(ListViewA.java:519)
07-12 19:29:38.006: W/System.err(8478): at com.example.kiva.ListViewA$RetreiveFeedTask.onPostExecute(ListViewA.java:1330)
07-12 19:29:38.006: W/System.err(8478): at com.example.kiva.ListViewA$RetreiveFeedTask.onPostExecute(ListViewA.java:1)
07-12 19:29:38.006: W/System.err(8478): at android.os.AsyncTask.finish(AsyncTask.java:417)
07-12 19:29:38.006: W/System.err(8478): at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-12 19:29:38.006: W/System.err(8478): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-12 19:29:38.016: W/System.err(8478): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 19:29:38.016: W/System.err(8478): at android.os.Looper.loop(Looper.java:130)
07-12 19:29:38.016: W/System.err(8478): at android.os.HandlerThread.run(HandlerThread.java:60)
07-12 19:29:40.096: W/FlurryAgent(8478): onEndSession called without context from corresponding onStartSession
07-12 19:29:40.206: E/WindowManager(8478): Activity com.example.kiva.ListViewA has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40631600 that was originally added here
07-12 19:29:40.206: E/WindowManager(8478): android.view.WindowLeaked: Activity com.example.kiva.ListViewA has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40631600 that was originally added here
07-12 19:29:40.206: E/WindowManager(8478): at android.view.ViewRoot.<init>(ViewRoot.java:258)
07-12 19:29:40.206: E/WindowManager(8478): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
07-12 19:29:40.206: E/WindowManager(8478): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-12 19:29:40.206: E/WindowManager(8478): at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-12 19:29:40.206: E/WindowManager(8478): at android.app.Dialog.show(Dialog.java:241)
07-12 19:29:40.206: E/WindowManager(8478): at android.app.ProgressDialog.show(ProgressDialog.java:107)
07-12 19:29:40.206: E/WindowManager(8478): at android.app.ProgressDialog.show(ProgressDialog.java:90)
07-12 19:29:40.206: E/WindowManager(8478): at com.example.kiva.ListViewA.topfeeds(ListViewA.java:1598)
07-12 19:29:40.206: E/WindowManager(8478): at com.example.kiva.ListViewA.onCreate(ListViewA.java:278)
07-12 19:29:40.206: E/WindowManager(8478): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-12 19:29:40.206: E/WindowManager(8478): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-12 19:29:40.206: E/WindowManager(8478): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-12 19:29:40.206: E/WindowManager(8478): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-12 19:29:40.206: E/WindowManager(8478): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-12 19:29:40.206: E/WindowManager(8478): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 19:29:40.206: E/WindowManager(8478): at android.os.Looper.loop(Looper.java:130)
07-12 19:29:40.206: E/WindowManager(8478): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-12 19:29:40.206: E/WindowManager(8478): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 19:29:40.206: E/WindowManager(8478): at java.lang.reflect.Method.invoke(Method.java:507)
07-12 19:29:40.206: E/WindowManager(8478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-12 19:29:40.206: E/WindowManager(8478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-12 19:29:40.206: E/WindowManager(8478): at dalvik.system.NativeStart.main(Native Method)