【问题标题】:Android App runs in wrong UI thread after kill app?杀死应用程序后,Android 应用程序在错误的 UI 线程中运行?
【发布时间】:2015-11-25 05:09:14
【问题描述】:

我的应用程序有一个 GCMIntentService 扩展在 GCMBaseIntentService 上。收到消息后,我将打开一个带有Webview 的活动。 这里是代码:

GCMIntentService:

public class GCMIntentService extends GCMBaseIntentService {
    @Override
    protected void onMessage(Context context, Intent intent) {
        Intent intent = new Intent(context, MyActivity.class);
        context.startActivity(intent);
    }
}

我的活动:

public class MyActiviy extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        webView = new WebView(MyActiviy.this); 
        /*This throw an exception "java.lang.IllegalStateException:
        Calling View methods on another thread than the UI thread."*/
    }
}

通常,此代码工作正常,但是当我在“最近的应用程序”上终止应用程序并推送消息时,在 MyActivity 上创建新的 webview 实例时应用程序崩溃。

我在下面尝试了这段代码:

public class MyActiviy extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                webView = new WebView(MyActiviy.this);
                boolean isRunOnUIThread = Looper.myLooper() == Looper.getMainLooper();
                Log.i("CheckThread","is running on UI thread: " + isRunOnUIThread )
                // This log return true
            }
        });

    }
}

但这不起作用。

崩溃日志:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package.name/my.package.name.MyActivity}: java.lang.IllegalStateException: Calling View methods on another thread than the UI thread.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.IllegalStateException: Calling View methods on another thread than the UI thread.
            at com.android.webview.chromium.WebViewChromium.createThreadException(WebViewChromium.java:287)
            at com.android.webview.chromium.WebViewChromium.checkThread(WebViewChromium.java:309)
            at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:220)
            at android.webkit.WebView.<init>(WebView.java:606)
            at android.webkit.WebView.<init>(WebView.java:542)
            at android.webkit.WebView.<init>(WebView.java:525)
            at android.webkit.WebView.<init>(WebView.java:512)
            at android.webkit.WebView.<init>(WebView.java:502)
            at my.package.name.MyActivity$2.run(MyActivity.java:254)
            at android.app.Activity.runOnUiThread(Activity.java:5511)
            at my.package.name.MyActivity.onCreate(MyActivity.java:246)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我猜我的应用程序在“错误的 UI 线程”上运行:D
或者WebWiewcheckThread()方法不对。

注意:此错误仅在我终止应用程序并推送消息后发生。

谁能帮我解决这个错误?非常感谢。

附加信息:此错误仅发生在 android L 及更高版本 (>= 5.0)

【问题讨论】:

  • mInstance 是什么?
  • 应用崩溃时的堆栈跟踪是什么?
  • 感谢您的回复,我已经编辑了我的问题

标签: android google-cloud-messaging ui-thread


【解决方案1】:

当您从服务开始活动时,请在您的意图中添加 Intent.FLAG_ACTIVITY_NEW_TASK 标志。

    Intent intent = new Intent(context, MyActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);

【讨论】:

    【解决方案2】:

    像这样更改启动活动的代码行:

    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
    
            @Override
            public void run() {
                Intent intent = new Intent(context, MyActivity.class);
                context.startActivity(intent);
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-23
      相关资源
      最近更新 更多