来自Javascript接口类的SetText

时间:2012-06-08 10:57:46

标签: android android-webview

我有一个javascript接口类来从javascript接收一些值。我从接口类中烘焙了该值,但我无法将该值设置为TextVie。会有什么问题??

 class JavaScriptInterfaceForCount {
    Context mContext;

    JavaScriptInterfaceForCount(Context c) {
        mContext = c;
    }
    public void receiveValueFromJs(String scrollwidth, String columwidth) {
            mPagenumberTextView.setText(scrollwidth+"");
      }
 }

Logcat结果

06-08 16:20:37.003: E/AndroidRuntime(1198):                android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created     a view hierarchy can touch its views.
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.view.ViewRoot.requestLayout(ViewRoot.java:594)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.view.View.requestLayout(View.java:8125)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.view.View.requestLayout(View.java:8125)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.view.View.requestLayout(View.java:8125)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.view.View.requestLayout(View.java:8125)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:254)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.view.View.requestLayout(View.java:8125)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.widget.TextView.checkForRelayout(TextView.java:5378)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.widget.TextView.setText(TextView.java:2688)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.widget.TextView.setText(TextView.java:2556)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.widget.TextView.setText(TextView.java:2531)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at com.epub.reader.TestWebActivity$JavaScriptInterfaceForCount.receiveValueFromJs(TestWebActivity.java:326)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.webkit.BrowserFrame.stringByEvaluatingJavaScriptFromString(Native Method)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.webkit.BrowserFrame.stringByEvaluatingJavaScriptFromString(Native Method)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.webkit.BrowserFrame.loadUrl(BrowserFrame.java:245)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.webkit.WebViewCore.loadUrl(WebViewCore.java:1562)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.webkit.WebViewCore.access$1400(WebViewCore.java:52)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:948)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.os.Looper.loop(Looper.java:123)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:621)
06-08 16:20:37.003: E/AndroidRuntime(1198):     at java.lang.Thread.run(Thread.java:1096)

1 个答案:

答案 0 :(得分:6)

runOnUiThread(new Runnable() {
     public void run() {
         mPagenumberTextView.setText(scrollwidth+"");
    }
});

您必须在主线程中编辑UI