2017-03-21 57 views
0

我正在開發一個應用程序,我正在使用webview。一切工作正常,但當我嘗試在webview中打開外部鏈接時,它會凍結或崩潰。這發生在android 4.4及以下版本,但在android 5.0及更高版本上運行良好。Android Webview在Android 4.4上崩潰,同時打開外部URL

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文件,我想知道爲什麼發生這種情況。

+1

顯示您的logcat – shmakova

+1

請嘗試用[Minimum,Complete and Verifiable Example](http://stackoverflow.com/help/mcve)替換您的MainActivity代碼。 – Haem

+0

@HeikkiMäenpää你是什麼意思?我很高興這個平臺和android開發。 –

回答

0

嘗試使用WebChromeClient;而不是WebViewClient;看看它是否有效。

相關問題