我很常常被这个错误搞糊涂了。应该从mainthread或UI线程调用UI元素或视图以及listview的适配器notifystatechange方法。我完全通过从Asctiask的onPostExecute方法调用它们来完成它,因为它在主线程上运行。我已经创建了一个广播接收器和onRecieve方法,我创建了一个从Asyntask扩展的匿名类。然而,我仍然得到这个错误,我不知道为什么。
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent != null) {
final String action = intent.getAction();
if(Constants.ACTION_SUCCESS_LAUNCHES.equals(action)) {
new LoadInBackground().execute();
} else if(Constants.ACTION_FAILURE_LAUNCHES.equals(action)) {
textView.setText("Poor Network Connection");
progressBar.setVisibility(View.GONE);
}
}
}
};
private class LoadInBackground extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
for(int i = 0; i < sharedPreference.getRockets().size(); i++) {
rocketsLocal.add(sharedPreference.getRockets().get(i));
rocketsGlobal.add(sharedPreference.getRockets().get(i));
}
if(sharedPreference.getPrefsNotifications())
setAlarm(sharedPreference.getRockets()
.get(0).getWsstamp());
return null;
}
@Override
protected void onPostExecute(Void v) {
//should be handled in main thread
adapterLocal.notifyDataSetChanged();
adapterGlobal.notifyDataSetChanged();
//should be handled in main thread
textView.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
}
}
LOG:
19:02:56 E AndroidRuntime : FATAL EXCEPTION: AsyncTask #1
19:02:56 E AndroidRuntime : Process: com.xyz, PID: 17500
19:02:56 E AndroidRuntime : java.lang.RuntimeException: An error occured while executing doInBackground()
19:02:56 E AndroidRuntime : at android.os.AsyncTask$3.done(AsyncTask.java:304)
19:02:56 E AndroidRuntime : at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
19:02:56 E AndroidRuntime : at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
19:02:56 E AndroidRuntime : at java.util.concurrent.FutureTask.run(FutureTask.java:242)
19:02:56 E AndroidRuntime : at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
19:02:56 E AndroidRuntime : at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
19:02:56 E AndroidRuntime : at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
19:02:56 E AndroidRuntime : at java.lang.Thread.run(Thread.java:818)
19:02:56 E AndroidRuntime : Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
19:02:56 E AndroidRuntime : at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6357)
19:02:56 E AndroidRuntime : at android.view.ViewRootImpl.focusableViewAvailable(ViewRootImpl.java:2944)
19:02:56 E AndroidRuntime : at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:685)
19:02:56 E AndroidRuntime : at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:685)
19:02:56 E AndroidRuntime : at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:685)
19:02:56 E AndroidRuntime : at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:685)
19:02:56 E AndroidRuntime : at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:685)
19:02:56 E AndroidRuntime : at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:685)
19:02:56 E AndroidRuntime : at android.view.View.setFlags(View.java:9691)
19:02:56 E AndroidRuntime : at android.view.View.setFocusableInTouchMode(View.java:6771)
19:02:56 E AndroidRuntime : at android.widget.AdapterView.checkFocus(AdapterView.java:727)
19:02:56 E AndroidRuntime : at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:822)
19:02:56 E AndroidRuntime : at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:6140)
19:02:56 E AndroidRuntime:在android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37) 19:02:56 E AndroidRuntime:在android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50) 19:02:56 E AndroidRuntime:在android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:286) 19:02:56 E AndroidRuntime:at com.studiotyche.apps.android.launchtracker.MainActivity $ LoadInBackground.doInBackground(MainActivity.java:322) 19:02:56 E AndroidRuntime:at com.studiotyche.apps.android.launchtracker.MainActivity $ LoadInBackground.doInBackground(MainActivity.java:310) 19:02:56 E AndroidRuntime:在android.os.AsyncTask $ 2.call(AsyncTask.java:292) 19:02:56 E AndroidRuntime:at java.util.concurrent.FutureTask.run(FutureTask.java:237) 19:02:56 E AndroidRuntime:......还有4个 19:02:56 E ActivityThread:活动com.studiotyche.apps.android.launchtracker.MainActivity泄露了最初在这里注册的IntentReceiver com.studiotyche.apps.android.launchtracker.MainActivity$4@90e6214。你是否错过了对unregisterReceiver()的调用? 19:02:56 E ActivityThread:android.app.IntentReceiverLeaked:Activity com.studiotyche.apps.android.launchtracker.MainActivity泄露了最初在这里注册的IntentReceiver com.studiotyche.apps.android.launchtracker.MainActivity$4@90e6214。你是否错过了对unregisterReceiver()的调用? 19:02:56 E ActivityThread:在android.app.LoadedApk $ ReceiverDispatcher。(LoadedApk.java:903) 19:02:56 E ActivityThread:在android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:704) 19:02:56 E ActivityThread:在android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1643) 19:02:56 E ActivityThread:在android.app.ContextImpl.registerReceiver(ContextImpl.java:1623) 19:02:56 E ActivityThread:在android.app.ContextImpl.registerReceiver(ContextImpl.java:1617) 19:02:56 E ActivityThread:在android.content.ContextWrapper.registerReceiver(ContextWrapper.java:489) 19:02:56 E ActivityThread:at com.studiotyche.apps.android.launchtracker.MainActivity.onCreate(MainActivity.java:124) 19:02:56 E ActivityThread:在android.app.Activity.performCreate(Activity.java:5990) 19:02:56 E ActivityThread:在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 19:02:56 E ActivityThread:在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 19:02:56 E ActivityThread:在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 19:02:56 E ActivityThread:在android.app.ActivityThread.access $ 800(ActivityThread.java:151) 19:02:56 E ActivityThread:在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) 19:02:56 E ActivityThread:在android.os.Handler.dispatchMessage(Handler.java:102) 19:02:56 E ActivityThread:在android.os.Looper.loop(Looper.java:135) 19:02:56 E ActivityThread:在android.app.ActivityThread.main(ActivityThread.java:5254) 19:02:56 E ActivityThread:at java.lang.reflect.Method.invoke(Native Method) 19:02:56 E ActivityThread:at java.lang.reflect.Method.invoke(Method.java:372) 19:02:56 E ActivityThread:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 19:02:56 E ActivityThread:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)