English 中文(简体)
Android WebView JavaScript 调回原始视图
原标题:Android WebView JavaScript callback to original View

我正在写一个机器人应用程序,其中我在网络视图中插入正本代码,然后通过调用“编码”webview.addJavacript Interface 所指定的java方法来回答正本回答。

问题是, javascript- recall 正在另一条线上运行, 而不是我启动此进程的原始视图。 因此我无法访问原始视图 。

我怎样才能进入原始视图?

我使用Api 7级。

以下是重要的代码碎片:

JavaScript 呼叫 :

webview.addJavascriptInterface(this, "ACall");
webview.loadUrl("javascript: ACall.update();");

回调 :

public void update() {
    TextView tv = (TextView) findViewById(R.id.tv);
    Log.v("test", "test");
    tv.setText("test");
}

和堆叠轨迹:

Uncaught handler: thread WebViewCoreThread exiting due to uncaught exception
ndroid.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRoot.checkThread(ViewRoot.java:2706)
at android.view.ViewRoot.requestLayout(ViewRoot.java:560)
at android.view.View.requestLayout(View.java:7918)
at android.view.View.requestLayout(View.java:7918)
at android.view.View.requestLayout(View.java:7918)
at android.view.View.requestLayout(View.java:7918)
at android.view.View.requestLayout(View.java:7918)
at android.widget.TextView.checkForRelayout(TextView.java:5380)
at android.widget.TextView.setText(TextView.java:2684)
at android.widget.TextView.setText(TextView.java:2552)
at android.widget.TextView.setText(TextView.java:2527)
at shylux.android.GameActivity.update(GameActivity.java:53)
at android.webkit.BrowserFrame.stringByEvaluatingJavaScriptFromString(Native Method)
at android.webkit.BrowserFrame.stringByEvaluatingJavaScriptFromString(Native Method)
at android.webkit.BrowserFrame.loadUrl(BrowserFrame.java:149)
at android.webkit.WebViewCore.loadUrl(WebViewCore.java:1449)
at android.webkit.WebViewCore.access$1400(WebViewCore.java:48)
at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:890)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:611)
at java.lang.Thread.run(Thread.java:1096)

////////////////////目前的解决办法:

这是我目前的解决办法。我认为必须有一个更好的办法。如果你想做不同的任务,这需要做很多工作。

public void update() {
    tv.post(new UpdateTask());
}

private class UpdateTask implements Runnable {
    public void run() {
        tv.setText("test");
    }
}
问题回答

Thanks, I had a similar problem. The solution with runOnUiThread was surprisingly simple.

无法从最初创建活动的线条外部更新活动的 UI。使用 run OnUithread () 方法进行 UI 更改。

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                button.setBackgroundResource(R.drawable.practice_ri_wrong_3);
            }
        });

< a href=" "http:// developmenter.android.com/ reference/android/app/activity.html#run OnUitthread%28java.lang. runnable%29" rel="nofollow" >run OnUitread 是你的朋友。只要做一个可运行的可运行的可运行的可运行的可操作的可操作文件,对您的观点做出必要的修改,然后通过它运行OnUithrefread。 < a href=>http:// developer.android.com/resources/articless/painless-threading.html" rel=“nofollowing" > nopain streing 文章就可以建议一些你可能考虑的其他选项(如果你对活动没有手,那么tv.post可能是一个猎物)。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签