在我的Activity的onCreate方法中,我注册了一个onSharedPreferenceChangeListener。活动本身实现了OnSharedPreferenceChangeListener。
更新首选项时,将调用以下方法。
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals(Configuration.PREF_TIMETABLE_UPDATING)){
resetView();
}
}
public void resetView(){
this.container.removeAllViews();
}
此代码在更新某个属性后从容器视图中删除所有视图。它适用于我的Android 4设备,但在我的Android 2.2.1设备上应用程序崩溃:
03-24 16:09:00.054: E/AndroidRuntime(1712): FATAL EXCEPTION: AsyncTask #5
03-24 16:09:00.054: E/AndroidRuntime(1712): java.lang.RuntimeException: An error occured while executing doInBackground()
03-24 16:09:00.054: E/AndroidRuntime(1712): at android.os.AsyncTask$3.done(AsyncTask.java:200)
03-24 16:09:00.054: E/AndroidRuntime(1712): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-24 16:09:00.054: E/AndroidRuntime(1712): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-24 16:09:00.054: E/AndroidRuntime(1712): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-24 16:09:00.054: E/AndroidRuntime(1712): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-24 16:09:00.054: E/AndroidRuntime(1712): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-24 16:09:00.054: E/AndroidRuntime(1712): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-24 16:09:00.054: E/AndroidRuntime(1712): at java.lang.Thread.run(Thread.java:1102)
03-24 16:09:00.054: E/AndroidRuntime(1712): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-24 16:09:00.054: E/AndroidRuntime(1712): at android.view.ViewRoot.checkThread(ViewRoot.java:2824)
03-24 16:09:00.054: E/AndroidRuntime(1712): at android.view.ViewRoot.requestLayout(ViewRoot.java:598)
03-24 16:09:00.054: E/AndroidRuntime(1712): at android.view.View.requestLayout(View.java:8126)
03-24 16:09:00.054: E/AndroidRuntime(1712): at android.view.View.requestLayout(View.java:8126)
03-24 16:09:00.054: E/AndroidRuntime(1712): at android.view.View.requestLayout(View.java:8126)
03-24 16:09:00.054: E/AndroidRuntime(1712): at android.view.View.requestLayout(View.java:8126)
03-24 16:09:00.054: E/AndroidRuntime(1712): at android.widget.ScrollView.requestLayout(ScrollView.java:1200)
03-24 16:09:00.054: E/AndroidRuntime(1712): at android.view.View.requestLayout(View.java:8126)
03-24 16:09:00.054: E/AndroidRuntime(1712): at android.view.ViewGroup.removeAllViews(ViewGroup.java:2263)
03-24 16:09:00.054: E/AndroidRuntime(1712): at com.example.TimetableActivity.resetView(TimetableActivity.java:430)
03-24 16:09:00.054: E/AndroidRuntime(1712): at com.example.TimetableActivity.onSharedPreferenceChanged(TimetableActivity.java:557)
03-24 16:09:00.054: E/AndroidRuntime(1712): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2869)
03-24 16:09:00.054: E/AndroidRuntime(1712): at com.example.MyHelper.setTimetableUpdate(MyHelper.java:145)
03-24 16:09:00.054: E/AndroidRuntime(1712): at com.example.webservice.task.LessonFetcherTask.doInBackground(LessonFetcherTask.java:33)
03-24 16:09:00.054: E/AndroidRuntime(1712): at com.example.webservice.task.LessonFetcherTask.doInBackground(LessonFetcherTask.java:1)
03-24 16:09:00.054: E/AndroidRuntime(1712): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-24 16:09:00.054: E/AndroidRuntime(1712): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-24 16:09:00.054: E/AndroidRuntime(1712): ... 4 more
如何确保OnSharedPreferenceChangeListener调用的方法在UI线程上运行?如何解决这个问题的任何帮助将不胜感激!
答案 0 :(得分:1)
问题是你不知何故徘徊在一个新线程上:
Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
确保UI命令在UI线程上的一种方法是使用UI处理程序。 (它并不像听起来那么难。)只需将removeViews()
放在Runnable中,并在任何View上使用post()
。像这样:
创建一个Runnable:
Runnable runnable = new Runnable() {
@Override
public void run() {
resetView();
}
};
将其发布到UI线程:
if(key.equals(Configuration.PREF_TIMETABLE_UPDATING)){
container.post(runnable);
}