Android WebView活动崩溃:java.lang.IllegalStateException:在除UI线程之外的另一个线程上调用View方法

时间:2017-05-21 10:24:32

标签: java android unity3d webview

我有一个用于统一的自定义Android插件,可以打开Webview个活动。当我尝试打开活动时,它会崩溃。当我尝试初始化WebView时,下面的日志语句出现在log cat中。

Caused by: java.lang.IllegalStateException: Calling View methods on another thread than the UI thread.
at com.android.webview.chromium.WebViewChromium.createThreadException(WebViewChromium.java:252)
at com.android.webview.chromium.WebViewChromium.checkThread(WebViewChromium.java:267)  
at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:163)
at android.webkit.WebView.<init>(WebView.java:661)
at android.webkit.WebView.<init>(WebView.java:596)
at android.webkit.WebView.<init>(WebView.java:579)
at android.webkit.WebView.<init>(WebView.java:566)
at android.webkit.WebView.<init>(WebView.java:556)
at com.vadrnet.unitysdk.MyWebViewClass.<init>(MyWebViewClass.java:31)
at com.vadrnet.unitysdk.WebViewActivity$1.run(WebViewActivity.java:207)
at android.app.Activity.runOnUiThread(Activity.java:5850)
at com.vadrnet.unitysdk.WebViewActivity.onCreate(WebViewActivity.java:205)
at android.app.Activity.performCreate(Activity.java:6582)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2532)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

我通过Unity Script启动活动的代码是

AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
currentActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
{
   AndroidJavaClass jc = new AndroidJavaClass("com.vadrnet.unitysdk.ManagerClass");
   AndroidJavaObject className = jc.CallStatic<AndroidJavaObject>("instance");
   className.Call("click", currentActivity, url);
 }));

和插件中的Click函数是

 public void click(Activity root, String url){
    Intent intent = new  Intent(root, WebViewActivity.class);
    intent.putExtra("url", url);
    root.startActivity(intent);
}

尽管在UI线程本身打开了活动,但我无法理解错误发生的原因。任何帮助都会很棒。

0 个答案:

没有答案