Android webview侦听错误和加载对话框

时间:2016-02-06 15:06:06

标签: android android-webview

根据一些备受推崇的答案(12),以下代码应在网页浏览中打开网站,然后在网页上侦听错误。然而,它在第一个障碍失败 - 网页视图中的网站(谷歌)没有加载(我肯定有互联网连接)。这是为什么?此外,如果您使用Android后退按钮打开抽屉并单击"再次尝试/刷新",抽屉成功消失并显示加载对话框,但随后它停止响应后退按钮。

MainActivity.java

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    ProgressDialog progress;

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

        WebView webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebViewClient(new WebViewClient() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
                view.loadUrl("http://www.google.com");
                return true;
            }
            @Override
            public void onPageStarted(WebView view, String url, Bitmap facIcon) {
                progress = ProgressDialog.show(MainActivity.this, "", "Loading...", true);
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                drawer.closeDrawer(GravityCompat.START);
                progress.dismiss();
            }
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                view.loadUrl("file:///android_asset/www/error.html");
                drawer.openDrawer(GravityCompat.START);
                progress.dismiss();
            }
        });

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            drawer.openDrawer(GravityCompat.START);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        WebView webView = (WebView) findViewById(R.id.webView);
        int id = item.getItemId();

        if (id == R.id.nav_refresh) {
            webView.loadUrl("javascript:window.location.reload(true)");
            progress = ProgressDialog.show(this, "", "Loading...", true);
        } else if (id == R.id.nav_home) {
            webView.loadUrl("http://www.google.com");
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

修正了它。错误发生在

finfo_file()

应该是

public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
    view.loadUrl("http://www.google.com");
    return true;
}

然后在setWebViewClient外加载实际的url:

public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
    view.loadUrl(urlNewString);
    return true;
}