2016-03-24 17 views
1

Uygulamamda bir web sayfasını görüntülemek için WebView kullanmaya çalışıyorum. İlk başta, AndroidManifest.xml dosyasında herhangi bir izin almadım ve hata 'web sayfası şu anda yüklenemedi çünkü net :: ERR_CACHE_MISS'WebView, İnternet izinleri verildikten sonra yüklenmiyor mu?

Ancak AndroidManifest'e bu izni ekledim. xml, WebView boş bir beyaz kutu olarak yüklenir.

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package= ... > 

<uses-permission android:name="android.permission.INTERNET" /> 

<application ... 
</application> 

</manifest> 

Ve Java sınıfı:

public class location_Fragment extends Fragment { 

private WebView webView; 

View rootview; 

@Nullable 
@Override 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    rootview = inflater.inflate(R.layout.location_layout, container, false); 
    webView = (WebView)rootview.findViewById(R.id.webView); 

    WebSettings webSettings = webView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 

    webView.loadUrl("https://www.google.com"); 
    return rootview; 
    } 
} 
+0

İşte

benim AndroidManifest.xml nedir? –

cevap

0
Try this: 
@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     rootview = inflater.inflate(R.layout.location_layout, container, false); 
     webView = (WebView)rootview.findViewById(R.id.webView); 
     webView.setWebViewClient(new MyBrowser()); 
     WebSettings webSettings = webView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 

     webView.loadUrl("https://www.google.com"); 
     return rootview; 
     } 
    } 

     private class MyBrowser extends WebViewClient { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       view.loadUrl(url); 
       return true; 
      } 
     } 
0

kullanın sizin webclient olduğunu takiben Kod

public class WebViewActivity extends AppCompatActivity { 
    private WebView webView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_web_view); 
     Bundle b = getIntent().getExtras(); 
     String data =b.getString("link"); 
     webView = (WebView) findViewById(R.id.webView1); 

     webView.setWebViewClient(new MyWebViewClient()); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.getSettings().setBuiltInZoomControls(true); 
     if(data.equals("")||data.equals("null")) { 
      Toast.makeText(WebViewActivity.this,"No Link Available",Toast.LENGTH_SHORT).show(); 
     } 
     { 
      webView.loadUrl(data); 
     } 
     webView.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) { 
       // Activities and WebViews measure progress with different scales. 
       // The progress meter will automatically disappear when we reach 100% 
       // activity.setProgress(progress * 1000); 
       WebViewActivity.this.setTitle("Loading..."); 
       WebViewActivity.this.setProgress(progress * 100); 
       if(progress == 100) 
        WebViewActivity.this.setTitle(R.string.app_name); 
      } 
     }); 
    } 

    private class MyWebViewClient extends WebViewClient { 
     private ProgressDialog pDialog; 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      // pDialog = new ProgressDialog(WebViewActivity.this,R.style.DialogTheme); 
      view.loadUrl(url); 
      return true; 
     } 
     //Show loader on url load 
     public void onLoadResource (WebView view, String url) { 
      if (pDialog == null) { 
       // in standard case YourActivity.this 
       pDialog = new ProgressDialog(WebViewActivity.this,R.style.DialogTheme); 
       pDialog.setMessage("Loading..."); 
       pDialog.show(); 
      } 
     } 

     @Override 
     public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { 
      handler.proceed(); 
     } 

     public void onPageFinished(WebView view, String url) { 
      try{ 
       pDialog.dismiss(); 
       if (pDialog.isShowing()) { 
        pDialog.dismiss(); 
        pDialog = null; 
       } 
      }catch(Exception exception){ 
       exception.printStackTrace(); 
      } 
     } 
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (event.getAction() == KeyEvent.ACTION_DOWN) { 
      switch (keyCode) { 
       case KeyEvent.KEYCODE_BACK: 
        if (webView.canGoBack()) { 
         webView.goBack(); 
        } else { 
         finish(); 
        } 
        return true; 
      } 

     } 
     return super.onKeyDown(keyCode, event); 
    } 

} 
İlgili konular