Android WebView不显示网站的内容

时间:2017-03-23 15:28:54

标签: javascript android webview android-webview

我是编程中的菜鸟,我正在通过跟踪网站进行WebView。问题是,当我在WebView中输入跟踪代码时,网站没有显示任何内容,我会在下一张图片中向您展示:

这是我的代码:

public class MainActivity extends AppCompatActivity {
private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    webview.addJavascriptInterface(new WebAppInterface(this), "android");
    webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

    progressBar = ProgressDialog.show(MainActivity.this, "WebView Example", "Loading...");

    webview.setWebViewClient(new WebViewClient(){
        public boolean shouldOverrideUrlLoading(WebView view, String url){
            Log.i(TAG, "Processing webview url click...");
            view.loadUrl(url);
            return true;
        }
        public void onPageFinished(WebView view, String url){
            Log.i(TAG, "Finished loading url: " +url);
            if (progressBar.isShowing()){
                progressBar.dismiss();
            }
        }
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
            Log.e(TAG, "Error: " + description);
            Toast.makeText(getApplicationContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
            alertDialog.setTitle("Error");
            alertDialog.setMessage(description);
            alertDialog.show();
        }
    });
    webview.loadUrl("https://geartrack.hdn.pt");
}
public class WebAppInterface{
    Context mContext;

    WebAppInterface(Context c){
        mContext = c;
    }

    @JavascriptInterface
    public void showToast(String toast){
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.mainmenu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){

        case R.id.action_refresh:
            webview.loadUrl("https://geartrack.hdn.pt");
            return true;
    }
    return super.onOptionsItemSelected(item);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()){
        webview.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
 }
}

请帮帮我。

1 个答案:

答案 0 :(得分:3)

问题您尚未为您的WebView启用LocalStorage。如果您通过开发者工具使用Chrome来inspect您的WebView,您会发现自己有错误。具体来说是main.js?v=1:373 Uncaught TypeError: Cannot read property 'length' of null

为什么会这样?如果您查看main.js中的该行代码,您会看到该脚本的访问长度为localStorage。它会使脚本崩溃,因为您无法访问未定义变量的属性。 localStorage未启用。

修复这个。为WebView启用LocalStorage添加以下行:

DomStorage

其中settings.setDomStorageEnabled(true);settings' s WebView对象。

这样做之后,一切都按预期工作!