2015-11-25 35 views
0

我的应用程序有GCMIntentService延伸GCMBaseIntentService。当收到消息时,我将打开一个有Webview的活动。 下面的代码:

GCMIntentServiceAndroid应用程序在杀死应用程序后在错误的UI线程中运行?

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

MyActiviy:

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."*/ 
    } 
} 

Normaly,此代码的工作很好,但是当我在 “最近使用的应用” 杀应用程序,推送消息,然后应用程序在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)

+2

什么是''mInstance? –

+2

什么是应用程序崩溃时的堆栈跟踪? –

+0

感谢您的回复,我已编辑我的问题 – NghiaDao

回答

0

当你开始从活动服务请您意图添加Intent.FLAG_ACTIVITY_NEW_TASK标志。

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

谢谢,我已经尝试过,这不起作用。 :( – NghiaDao

0

更改密码启动活动的像行:

Handler handler = new Handler(Looper.getMainLooper()); 
handler.post(new Runnable() { 

     @Override 
     public void run() { 
      Intent intent = new Intent(context, MyActivity.class); 
      context.startActivity(intent); 
     } 
    }); 
相关问题