2016-07-26 55 views
0

我遇到了嚴重的問題時,與Android的WebView負載網址,如果頁面無法加載URL和頁面是空白的,使用WebView其他頁面沒有加載了頁面,我必須退出我的應用程序重新啓動它,以便WebView頁面可以加載url成功。我不知道爲什麼會發生這種情況?的Android的WebView不能加載任何URL

有人能告訴我爲什麼和如何解決這個問題?

謝謝。

+0

請提供一些您已經嘗試過的代碼。 – mjosh

回答

2

試試這個在您的Mainfest活動設置此

android:onHistory="true" 

如果你有問題時也不會產生比試試這個代碼也

上創建

webview = (WebView) findViewById(R.id.webview); 
    WebSettings settings = webview.getSettings(); 
    settings.setJavaScriptEnabled(true); 
    //ws.setJavaScriptEnabled(true); 
    settings.setJavaScriptCanOpenWindowsAutomatically(true); 
    webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 




webview.setWebViewClient(new WebViewClient() { 

     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      Log.i(TAG, "Processing webview url click..."+url); 
      view.loadUrl(url); 

      return true; 
     } 


     public void onPageFinished(WebView view, String url) { 
      Log.e(TAG, "Finished loading URL: " + url); 


      //webview.reload(); 
     } 


     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 



     } 
    }); 

    webview.loadUrl("your Url"); 

裏面的類

@Override 
public void onBackPressed() { 

    if (webview.canGoBack()) { 
     webview.goBack(); 
    } else { 
     finish(); 
    } 


} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     //Display confirmation here, finish() activity. 
     if (webview.canGoBack()) { 
      webview.goBack(); 
     } else { 
      finish(); 
     } 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
    } 
+0

親愛的阿瓊賽尼,謝謝你回答我的問題,但您的代碼不幫我解決我的問題,我的代碼是類似於你,但錯誤依然存在,我很疑惑... –

+0

有你把機器人: onHistory =「true」在AndroidMainfiest中對你的活動 –

+0

我加了這行但軟件給我提示它是未知屬性,爲什麼?這條線是什麼意思? –

0

試一下這個代碼可以工作

WebViewSampleActivity.java

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.ProgressDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Window; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

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

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    setContentView(R.layout.main); 

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

    WebSettings settings = webview.getSettings(); 
    settings.setJavaScriptEnabled(true); 
    webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 

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

    progressBar = ProgressDialog.show(WebViewSampleActivity.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(); 
      } 
     } 

     @SuppressWarnings("deprecation") 
     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
      Log.e(TAG, "Error: " + description); 
      Toast.makeText(WebViewSampleActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
      alertDialog.setTitle("Error"); 
      alertDialog.setMessage(description); 
      alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        return; 
       } 
      }); 
      alertDialog.show(); 
     } 
    }); 
    webview.loadUrl("http://www.google.com"); 
    } 
} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<WebView 
    android:id="@+id/webView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" /> 

</LinearLayout> 

在清單中添加Internet許可

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

哦不,你的回答並不能解決我的問題。我的代碼和你一樣,我遇到的問題有時還會發生,但是一路感謝。 –