gpt4 book ai didi

java - android webview.loadUrl 不会加载另一个网页

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:53:17 25 4
gpt4 key购买 nike

在我简单的 android 应用程序中,我以编程方式在 WebView 中加载网页。它最初以默认网页开始,下一个网页根据第一个网页的用户输入加载。 JavaScript 通过消息将信息传递回 Android 端。一切正常,除非无论我做什么以及我提供哪个 URL,第二个网页都无法加载。当我加载第二个测试时,出现警告“必须在同一线程上调用所有 WebView 方法”。但据我所知,我没有使用多线程,也不需要使用多线程。

相关代码如下:

public class MainActivity extends Activity {
public WebView myWebView;
public int state;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_main);

myWebView = (WebView) findViewById(R.id.webview);

WebSettings myWebViewSettings = myWebView.getSettings();
myWebViewSettings.setJavaScriptEnabled(true);
myWebViewSettings.setDomStorageEnabled(true);
myWebViewSettings.setAllowFileAccessFromFileURLs(true);
myWebViewSettings.setAllowUniversalAccessFromFileURLs(true);

myWebView.addJavascriptInterface(new JavascriptHandler(), "cpjs");

state = 0;
loadNextTest();
}

public void webviewLoadURL(String url) {
Log.d("app", "now loading " + url);
myWebView.clearHistory();
myWebView.clearFormData();
myWebView.clearCache(true);
myWebView.loadUrl(url);
}

final class JavascriptHandler {
@JavascriptInterface
public void sendToAndroid(String text) {
if (text.equals("confirmed at target")) {
loadNextTest();
}
}
}

public void loadNextTest() {
Log.d("app", "now loading test " + (state + 1));
if (state == 0) {
webviewLoadURL("file:///android_asset/test1.html");
state = state + 1;

} else if (state == 1) {
webviewLoadURL("file:///android_asset/test2.html");
// webviewLoadURL("http://www.google.com");
state = state + 1;

} else {
webviewLoadURL("file:///android_asset/test3.html");
}
}
}

这是相关的日志行:

02-22 09:12:32.250: V/WebViewChromium(15117): Binding Chromium to the background looper Looper (main, tid 1) {41c7ec00}
02-22 09:12:32.250: I/chromium(15117): [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
02-22 09:12:32.255: I/BrowserProcessMain(15117): Initializing chromium process, renderers=0
02-22 09:12:32.265: W/chromium(15117): [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
02-22 09:12:32.315: D/dalvikvm(15117): GC_FOR_ALLOC freed 86K, 5% free 3217K/3364K, paused 8ms, total 8ms
02-22 09:12:32.315: I/dalvikvm-heap(15117): Grow heap (frag case) to 4.273MB for 1127536-byte allocation
02-22 09:12:32.325: D/dalvikvm(15117): GC_FOR_ALLOC freed <1K, 4% free 4318K/4468K, paused 11ms, total 11ms
02-22 09:12:32.345: D/dalvikvm(15117): GC_CONCURRENT freed <1K, 4% free 4317K/4468K, paused 1ms+6ms, total 17ms
02-22 09:12:32.365: D/app(15117): now loading test 1
02-22 09:12:32.365: D/app(15117): now loading file:///android_asset/test1.html
02-22 09:12:32.450: D/mali_winsys(15117): new_window_surface returns 0x3000
02-22 09:12:32.460: I/Icing(794): Indexing 5AA949AFB589F1D17D8668589402D01E615E228D from com.google.android.googlequicksearchbox
02-22 09:12:32.500: D/OpenGLRenderer(15117): Enabling debug mode 0
02-22 09:12:32.505: W/AwContents(15117): nativeOnDraw failed; clearing to background color.
02-22 09:12:32.540: I/ActivityManager(447): Displayed com.example.myApp/.MainActivity: +344ms
02-22 09:12:32.550: W/AwContents(15117): nativeOnDraw failed; clearing to background color.
02-22 09:12:32.635: I/Icing(794): Indexing done 5AA949AFB589F1D17D8668589402D01E615E228D
02-22 09:12:32.945: I/chromium(15117): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
02-22 09:12:32.960: I/chromium(15117): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
02-22 09:12:33.180: E/AndroidProtocolHandler(15117): Unable to open asset URL: file:///android_asset/backend/images/ajax-loader.gif
02-22 09:12:46.010: D/app(15117): now loading test 2
02-22 09:12:46.010: D/app(15117): now loading http://www.google.com
02-22 09:12:46.020: W/WebView(15117): java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {41c7ec00} called on Looper (JavaBridge, tid 271) {41c6f5c8}, FYI main Looper is Looper (main, tid 1) {41c7ec00})
02-22 09:12:46.020: W/WebView(15117): at android.webkit.WebView.checkThread(WebView.java:2063)
02-22 09:12:46.020: W/WebView(15117): at android.webkit.WebView.clearHistory(WebView.java:1399)
02-22 09:12:46.020: W/WebView(15117): at com.example.myApp.MainActivity.webviewLoadURL(MainActivity.java:68)
02-22 09:12:46.020: W/WebView(15117): at com.example.myApp.MainActivity.loadNextTest(MainActivity.java:91)
02-22 09:12:46.020: W/WebView(15117): at com.example.myApp.MainActivity$JavascriptHandler.sendToAndroid(MainActivity.java:78)
02-22 09:12:46.020: W/WebView(15117): at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
02-22 09:12:46.020: W/WebView(15117): at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
02-22 09:12:46.020: W/WebView(15117): at android.os.Handler.dispatchMessage(Handler.java:102)
02-22 09:12:46.020: W/WebView(15117): at android.os.Looper.loop(Looper.java:136)
02-22 09:12:46.020: W/WebView(15117): at android.os.HandlerThread.run(HandlerThread.java:61)
02-22 09:12:46.030: W/System.err(15117): java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {41c7ec00} called on Looper (JavaBridge, tid 271) {41c6f5c8}, FYI main Looper is Looper (main, tid 1) {41c7ec00})
02-22 09:12:46.035: W/System.err(15117): at android.webkit.WebView.checkThread(WebView.java:2073)
02-22 09:12:46.035: W/System.err(15117): at android.webkit.WebView.clearHistory(WebView.java:1399)
02-22 09:12:46.035: W/System.err(15117): at com.example.myApp.MainActivity.webviewLoadURL(MainActivity.java:68)
02-22 09:12:46.035: W/System.err(15117): at com.example.myApp.MainActivity.loadNextTest(MainActivity.java:91)
02-22 09:12:46.035: W/System.err(15117): at com.example.myApp.MainActivity$JavascriptHandler.sendToAndroid(MainActivity.java:78)
02-22 09:12:46.035: W/System.err(15117): at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
02-22 09:12:46.040: W/System.err(15117): at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
02-22 09:12:46.040: W/System.err(15117): at android.os.Handler.dispatchMessage(Handler.java:102)
02-22 09:12:46.040: W/System.err(15117): at android.os.Looper.loop(Looper.java:136)
02-22 09:12:46.040: W/System.err(15117): at android.os.HandlerThread.run(HandlerThread.java:61)
02-22 09:12:46.040: W/System.err(15117): Caused by: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {41c7ec00} called on Looper (JavaBridge, tid 271) {41c6f5c8}, FYI main Looper is Looper (main, tid 1) {41c7ec00})
02-22 09:12:46.045: W/System.err(15117): at android.webkit.WebView.checkThread(WebView.java:2063)
02-22 09:12:46.045: W/System.err(15117): ... 9 more

感谢任何帮助。谢谢。

最佳答案

正如 T 教授所说,WebView 在幕后运行着许多线程,通过 JavaScript 接口(interface)对象从 JavaScript 回调是在后台线程上进行的。

您可以在 loadNextTest 中使用如下代码实现您喜欢的效果:

myWebView.post(new Runnable() {
@Override
public void run() {
webviewLoadURL("file:///android_asset/test1.html");
}
});

另请注意,您的 state 变量也将写入 JavaScript 后台线程,因此如果它们也来自其他线程,您需要同步您的读/写。

关于java - android webview.loadUrl 不会加载另一个网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21955593/

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