我的应用程序有一个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);
}
}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上。
或者checkThread()的WebWiew方法是错误的。
注意:这个错误只有在我杀死应用程序并推送消息之后才会发生。
有人能帮我解决这个问题吗?非常感谢。
附加信息:此bug仅发生在android和up (>= 5.0)上。
发布于 2015-11-25 05:22:13
当您从服务开始活动时,请在您的意图中添加Intent.FLAG_ACTIVITY_NEW_TASK标志。
Intent intent = new Intent(context, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);发布于 2015-12-01 17:50:23
更改代码开始活动行,如下所示:
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(context, MyActivity.class);
context.startActivity(intent);
}
});https://stackoverflow.com/questions/33908837
复制相似问题