WebView

时间:2017-04-18 14:48:41

标签: javascript android html webview onclick

我有一个显示本地HTML文件的WebView。在那个HTML中,我有一个硬编码的JavaScript函数。从一个按钮Onclick也可以在HTML-File中将函数传递给字符串到JavaScriptInterface。从那里我想在我的活动中调用一个方法与这两个交付的字符串。这可能吗?

我已经设置了所有内容并且它正在工作但是在活动中调用方法(在JSInterface中)只有在我将活动方法设置为静态以及何时设置时才有效static,我无法在静态方法中调用Activity中的其他方法(从JSInterface触发)。

这是我的代码,我希望你能帮助我:

public class JSWebAppInterface {
Context mContext;

/** Instantiate the interface and set the context */
JSWebAppInterface(Context c) {
    mContext = c;
}

@JavascriptInterface
public void sendData(String action, String filename){
    MbaiTextActivity.getDecision(action, filename);
}

}

public class MbaiTextActivity extends Activity {

private static final String TAG = "_TEXTACTIVITY";

MbaiWebView mwv;

RelativeLayout containerForWebViews;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text_activity);

    containerForWebViews = (RelativeLayout) findViewById(R.id.webView_container);

    mwv = new MbaiWebView(this);
    mwv.setWebViewClient(new MbaiWebViewClient(this));
    mwv.setOnStatusListener(onWebViewListener);
    mwv.addJavascriptInterface(new JSWebAppInterface(this), "android");
    mwv.findViewById(R.id.webView_container);

    mwv.loadHTML(textsPath + storyparts.get(currentPosition).getFilename());
    containerForWebViews.addView(mwv);

private MbaiWebView.OnStatusListener onWebViewListener = new MbaiWebView.OnStatusListener()

static void getDecision(String action, String filename){
    // just for test purpose
    System.out.println("Aus der TextAct.: " + filename + " " + action);
    // do soemthing
}

}

在JSInterface类中,我在Activity:

中调用了test()
    public void test(String one, String two){
    System.out.println("Aus der Test: " + one + " " + two);
    mwv.loadHTML(textsPath + "derBrax_part5.html");
}


04-18 08:01:23.248 17751-17891/com.example.max.mybai I/System.out: Aus der Test: Springen derBrax_part1.html

04-18 08:01:23.249 17751-17891 / com.example.max.mybai W / WebView:java.lang.Throwable:在线程' JavaBridge'上调用WebView方法。必须在同一个线程上调用所有WebView方法。 (预期的Looper Looper(主,tid 1){76eeef3}呼叫Looper(JavaBridge,tid 335){6f228eb},FYI主Looper是Looper(主,tid 1){76eeef3})                                                                     在android.webkit.WebView.checkThread(WebView.java:2334)                                                                     在android.webkit.WebView.loadUrl(WebView.java:927)                                                                     在com.example.max.mybai.MbaiWebView.loadHTML(MbaiWebView.java:94)                                                                     at com.example.max.mybai.MbaiTextActivity $ override.test(MbaiTextActivity.java:180)                                                                     at com.example.max.mybai.MbaiTextActivity $ override.access $ dispatch(MbaiTextActivity.java)                                                                     at com.example.max.mybai.MbaiTextActivity.test(MbaiTextActivity.java:0)                                                                     at com.example.max.mybai.JSWebAppInterface.sendData(JSWebAppInterface.java:24)                                                                     at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)                                                                     在org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:39)                                                                     在android.os.Handler.dispatchMessage(Handler.java:102)                                                                     在android.os.Looper.loop(Looper.java:154)                                                                     在android.os.HandlerThread.run(HandlerThread.java:61) 04-18 08:01:23.249 17751-17891 / com.example.max.mybai W / System.err:java.lang.RuntimeException:java.lang.Throwable:在线程' JavaBridge&#上调用WebView方法39 ;.必须在同一个线程上调用所有WebView方法。 (预期的Looper Looper(主,tid 1){76eeef3}呼叫Looper(JavaBridge,tid 335){6f228eb},FYI主Looper是Looper(主,tid 1){76eeef3}) 04-18 08:01:23.249 17751-17891 / com.example.max.mybai W / System.err:at android.webkit.WebView.checkThread(WebView.java:2344) 04-18 08:01:23.250 17751-17891 / com.example.max.mybai W / System.err:at android.webkit.WebView.loadUrl(WebView.java:927) 04-18 08:01:23.250 17751-17891 / com.example.max.mybai W / System.err:at com.example.max.mybai.MbaiWebView.loadHTML(MbaiWebView.java:94) 04-18 08:01:23.250 17751-17891 / com.example.max.mybai W / System.err:at com.example.max.mybai.MbaiTextActivity $ override.test(MbaiTextActivity.java:180) 04-18 08:01:23.250 17751-17891 / com.example.max.mybai W / System.err:at com.example.max.mybai.MbaiTextActivity $ override.access $ dispatch(MbaiTextActivity.java) 04-18 08:01:23.250 17751-17891 / com.example.max.mybai W / System.err:at com.example.max.mybai.MbaiTextActivity.test(MbaiTextActivity.java:0) 04-18 08:01:23.250 17751-17891 / com.example.max.mybai W / System.err:at com.example.max.mybai.JSWebAppInterface.sendData(JSWebAppInterface.java:24) 04-18 08:01:23.250 17751-17891 / com.example.max.mybai W / System.err:at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method) 04-18 08:01:23.250 17751-17891 / com.example.max.mybai W / System.err:at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:39) 04-18 08:01:23.250 17751-17891 / com.example.max.mybai W / System.err:at android.os.Handler.dispatchMessage(Handler.java:102) 04-18 08:01:23.250 17751-17891 / com.example.max.mybai W / System.err:at android.os.Looper.loop(Looper.java:154) 04-18 08:01:23.250 17751-17891 / com.example.max.mybai W / System.err:at android.os.HandlerThread.run(HandlerThread.java:61) 04-18 08:01:23.250 17751-17891 / com.example.max.mybai W / System.err:引起:java.lang.Throwable:在线程' JavaBridge'上调用了一个WebView方法。必须在同一个线程上调用所有WebView方法。 (预期的Looper Looper(主,tid 1){76eeef3}呼叫Looper(JavaBridge,tid 335){6f228eb},FYI主Looper是Looper(主,tid 1){76eeef3}) 04-18 08:01:23.250 17751-17891 / com.example.max.mybai W / System.err:at android.webkit.WebView.checkThread(WebView.java:2334) 04-18 08:01:23.250 17751-17891 / com.example.max.mybai W / System.err:... 11更多 04-18 08:01:23.255 17751-17751 / com.example.max.mybai I / chromium:[INFO:CONSOLE(19)]"未捕获错误:在方法调用期间引发了Java异常",source :file:///android_asset/books/der_brax/texts/derBrax_part1.html(19) 04-18 08:01:23.291 17751-17751 / com.example.max.mybai W / art:尝试删除非JNI本地引用,转储线程 04-18 08:01:23.432 17751-17751 / com.example.max.mybai W / art:尝试删除非JNI本地引用,转储线程

1 个答案:

答案 0 :(得分:0)

重写你的代码:

public void getDecision(String action, String filename){
    // just for test purpose
    System.out.println("Aus der TextAct.: " + filename + " " + action);
    // do soemthing
}

public class JSWebAppInterface {
    private final MbaiTextActivity act;

    /** Instantiate the interface and set the context */
    public JSWebAppInterface(MbaiTextActivity act) {
        this.act = act;
    }

    @JavascriptInterface
    public void sendData(String action, String filename){

        //EDIT:
        act.runOnUiThread(new Runnable() {

            @Override
            public void run () {

                 act.getDecision(action, filename);
            }
        });

    }
}

//in onCreate
mwv.addJavascriptInterface(new JSWebAppInterface(this), "android");