java webView.loadUrl 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10358045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:42:00  来源:igfitidea点击:

webView.loadUrl is not working

javaandroidhtmlwebview

提问by user1362015

This is my code:

这是我的代码:

package sai.datla.game;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class GametestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.clearSslPreferences();
        webView.loadUrl("file:///android_asset/www/index.html");
   }
}

but it is saying that it cannot find the link in the emulator. I have checked many times if my index.html file is in the www folder, which is in my assets, but it won't work. Please help.

但它说它在模拟器中找不到链接。我已经多次检查我的 index.html 文件是否在我的资产中的 www 文件夹中,但它不起作用。请帮忙。

By the way I am only twelve years old so please make the answers easy to understand for a child.

顺便说一下,我只有十二岁,所以请让孩子容易理解答案。

回答by Sreedev R

wbb = (WebView) findViewById(R.id.webView_tobe_loaded);    
WebSettings wbset=wbb.getSettings();
wbset.setJavaScriptEnabled(true);
wbb.setWebViewClient(new MyWebViewClient());
String url="http://www.google.com";
wbb.loadUrl(url);

This code will help you to solve the problem.It worked for me.

此代码将帮助您解决问题。它对我有用。

回答by Shankar Agarwal

instead of loadUrl, try using the loadDataWithBaseURL method:

代替 loadUrl,尝试使用 loadDataWithBaseURL 方法:

webview.loadDataWithBaseURL("android.resource://<package_name>/assets/www/file_name", html, mimeType, encoding, "");