我的 android 应用程序使用基于 PhoneGap 的Activity
登录。但是在用户登录后,在下一个活动中,我在后台运行了一个任务。但是当登录成功方法尝试编辑视图时(PhoneGap 活动关闭后),它总是抛出
android.view.ViewRoot$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。
我的代码是:
// BeginActivity extends DroidGap
public class BeginLoginActivity extends BeginActivity
{
@Override
protected String getFirstUrl() {
Intent intent = getIntent();
String pageState = intent.getStringExtra(SplashActivity.EXTRA_PAGE_STATE);
if (pageState == null) {
pageState = "login";
}
return "file:///android_asset/www/login.html#" +pageState;
}
//invoked at login.html via javascript
public void gotoMain() {
Intent intent = new Intent(this, MainNative.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
}
在 MainNative 类中,我编写了一个在后台运行的任务
public class MainNative extends Activity {
...
public void loadItems() {
...
new WatchlistHelper (this).execute(getItemId());
}
public void setWatchedStatus(Boolean _true) {
// this is where the WebViewCoreThread is thrown,
// whenever a phonegap based Activity previously has been opened
// and it run well if there's no phonegap Activity has run
watchlistButton_.setImageResource(_true ? R.drawable.native_rating_important
: R.drawable.native_rating_not_important);
watchlistButton_.setTag(_true);
watchlistButton_.setVisibility(VISIBLE);
watchlistLoading_.setVisibility(GONE);
}
...
}
监视列表助手:
public static class WatchlistHelper extends AsyncTask<String, Void, Boolean>
{
private MainNative mContext_;
...
@Override
protected Boolean doInBackground(String... _ids) {
// My code that run in background
return isTrue;
}
@Override
protected void onPostExecute(Boolean _isTrue) {
mContext_.setWatchedStatus(_isTrue);
}
}
来自 logcat 的日志:
06-21 14:45:57.462: E/AndroidRuntime(265): FATAL EXCEPTION: WebViewCoreThread
06-21 14:45:57.462: E/AndroidRuntime(265): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
06-21 14:45:57.462: E/AndroidRuntime(265): at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.view.ViewRoot.requestLayout(ViewRoot.java:594)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.view.View.requestLayout(View.java:8125)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.view.View.requestLayout(View.java:8125)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.view.View.requestLayout(View.java:8125)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:254)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.view.View.requestLayout(View.java:8125)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.view.View.requestLayout(View.java:8125)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.view.View.requestLayout(View.java:8125)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.widget.ScrollView.requestLayout(ScrollView.java:1200)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.view.View.requestLayout(View.java:8125)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:254)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.view.View.requestLayout(View.java:8125)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.view.View.requestLayout(View.java:8125)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:254)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.view.View.requestLayout(View.java:8125)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.view.View.requestLayout(View.java:8125)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.widget.ImageView.setImageResource(ImageView.java:275)
06-21 14:45:57.462: E/AndroidRuntime(265): at com.posaurus.android.elements.MainNative.setWatchedStatus(MainNative.java:160)
06-21 14:45:57.462: E/AndroidRuntime(265): at com.posaurus.android.elements.MainNative$WatchlistHelper.onPostExecute(MainNative.java:205)
06-21 14:45:57.462: E/AndroidRuntime(265): at com.posaurus.android.elements.MainNative$WatchlistHelper.onPostExecute(MainNative.java:1)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.os.AsyncTask.finish(AsyncTask.java:417)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.os.AsyncTask.access$300(AsyncTask.java:127)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.os.Looper.loop(Looper.java:123)
06-21 14:45:57.462: E/AndroidRuntime(265): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:621)
06-21 14:45:57.462: E/AndroidRuntime(265): at java.lang.Thread.run(Thread.java:1096)