gpt4 book ai didi

android 应用程序在 webview url 更改时崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:29:50 25 4
gpt4 key购买 nike

类似于此question ,但解决方案不适用于 mycase。

Android 应用在运行函数 webview.loadUrl 时崩溃
关闭时出现以下错误:

E/AndroidRuntime(1593): Caused by: java.lang.Throwable: Warning: A WebView method was called on thread 'WebViewCoreThread'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.

编辑:现在尝试在 runOnUiThread(new Runnable(){ }) 中运行该函数,但出现错误并且无法在上下文中输入 runOnUiThread 的位置。
完整代码如下:

   public class MyJavaScriptInterface {  
@JavascriptInterface
public void androidFieldPrompt(String title, String msg, final String funct) {
final EditText input = new EditText(MainActivity.this);
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle(title);
alert.setMessage(msg);
alert.setView(input);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();

// ERROR IS BEING THROWN HERE
webView.loadUrl("javascript:window."+funct+"('"+value+"')");

return;
}
});
alert.show();
}
}

最佳答案

E/AndroidRuntime(1593): Caused by: java.lang.Throwable: Warning: A WebView method was called on thread 'WebViewCoreThread'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.

正如错误指出的那样......您正在非 UI 线程上调用 webview 方法(在点击监听器内)。

你应该使用runOnUiThread在 onClick 内部调用 Web View 方法时使用主线程

  MainActivity.this.runOnUiThread(new Runnable(){
@Override
public void run() {
String value = input.getText().toString();
webView.loadUrl("javascript:window."+funct+"('"+value+"')");
return;
}
});

关于android 应用程序在 webview url 更改时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19600772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com