Android Webview在Android 4.4上打开外部url时崩溃

Android Webview在Android 4.4上打开外部url时崩溃,android,webview,Android,Webview,我正在开发一个使用webview的应用程序。一切正常,但当我试图打开webview内部的外部链接时,它要么冻结,要么崩溃。这种情况发生在android 4.4及以下版本上,但在android 5.0及以上版本上效果良好 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { ///initialize webview

我正在开发一个使用webview的应用程序。一切正常,但当我试图打开webview内部的外部链接时,它要么冻结,要么崩溃。这种情况发生在android 4.4及以下版本上,但在android 5.0及以上版本上效果良好

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    ///initialize webview
    private WebView mwebview;
    ProgressBar bar;
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        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);

        mwebview = (WebView) findViewById(R.id.myWebview);
        bar = (ProgressBar) findViewById(R.id.progressBar2);
        WebSettings webSettings = mwebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mwebview.loadUrl("http://stalls.co.ke/shopping");

        // improve webview perfomance

        mwebview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        if (Build.VERSION.SDK_INT >= 19) {
            mwebview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else {
            mwebview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }

        mwebview.getSettings().setAppCacheEnabled(true);
        mwebview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setSavePassword(true);
        webSettings.setSaveFormData(true);
        webSettings.setEnableSmoothTransition(true);

        //force links to open in webview only
        mwebview.setWebViewClient(new MyWebViewClient());



        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

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




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }




    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_home) {
            // Handle the camera action
            mwebview.loadUrl("http://stalls.co.ke/shopping");
        } else if (id == R.id.nav_gifts) {
            mwebview.loadUrl("http://stalls.co.ke/gifts-coupons");


        } else if (id == R.id.nav_men) {

            mwebview.loadUrl("http://stalls.co.ke/stallswear-men");

        } else if (id == R.id.nav_women) {

            mwebview.loadUrl("http://stalls.co.ke/stallswear-women");

        } else if (id == R.id.nav_kids) {

            mwebview.loadUrl("http://stalls.co.ke/stallswear-kids-baby");

        } else if (id == R.id.nav_men_cosmetics) {

            mwebview.loadUrl("http://stalls.co.ke/copy-of-stalls-cosmetics");

        } else if (id == R.id.nav_women_cosmetics) {

            mwebview.loadUrl("http://stalls.co.ke/copy-of-men-footwear");

        } else if (id == R.id.nav_kids_cosmetics) {

            mwebview.loadUrl("http://stalls.co.ke/copy-of-stalls-cosmetics");

        } else if (id == R.id.nav_homes) {

            mwebview.loadUrl("http://stalls.co.ke/home-kitchen-products");

        } else if (id == R.id.nav_garden) {

            mwebview.loadUrl("http://stalls.co.ke/garden-products");

        } else if (id == R.id.nav_smarthomes) {

            mwebview.loadUrl("http://stalls.co.ke/smart-home-products");

        } else if (id == R.id.nav_eatery) {

            mwebview.loadUrl("http://stalls.co.ke/stallseatery");


        } else if (id == R.id.nav_customer_service) {

            mwebview.loadUrl("http://stalls.co.ke/help");

        } else if (id == R.id.nav_about_us) {

            mwebview.loadUrl("http://stalls.co.ke/about-us");

        } else if (id == R.id.nav_kijo) {

            mwebview.loadUrl("http://stalls.co.ke/house-of-kinjo");

        } else if (id == R.id.nav_Airi) {

            mwebview.loadUrl("http://stalls.co.ke/Airi");

        } else if (id == R.id.nav_mamapeter) {

            mwebview.loadUrl("http://stalls.co.ke/house-of-kinjo");

        } else if (id == R.id.nav_swahili) {

            mwebview.loadUrl("http://stalls.co.ke/swahili-princess");

        } else if (id == R.id.nav_allstalls) {

            mwebview.loadUrl("http://stalls.co.ke/kenyan-stores");

        }








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

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
            .setName("Main Page") // TODO: Define a title for the content shown.
            // TODO: Make sure this auto-generated URL is correct.
            .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
            .build();
        return new Action.Builder(Action.TYPE_VIEW)
            .setObject(object)
            .setActionStatus(Action.STATUS_TYPE_COMPLETED)
            .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }


    // go to previous page when back button is pressed

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if (mwebview.canGoBack()) {
                        mwebview.goBack();
                    } else {
                        finish();
                    }
                    return true;

            }
        }

        return super.onKeyDown(keyCode, event);
    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            bar.setVisibility(view.GONE);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                mwebview.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            view.loadUrl(request.getUrl().toString());
                        }
                        return false;
                    }
                });
            } else {
                mwebview.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(url);
                        return true;
                    }
                });
            }
            return false;
        }
    }
}

这是我的
MainActivity.java文件,我想知道为什么会发生这种情况。

尝试使用WebChromeClient;而不是WebViewClient;看看它是否有效。

显示您的日志请尝试将您的主要活动代码替换为。@HeikkiMäenpä你是什么意思?我对这个平台和android开发有点陌生。可能与此类似,请检查它@kennedykiya尝试包括像这样的www…www.stalls。。。与现有URL一起,而不是