2013-07-01 49 views
1

我有與加載的URL WeView的活動,我想,以顯示該網站的加載過程中一點點等待的對話,所以我已經試過這樣:PrgressDialog當負載的WebView崩潰問題

private ProgressDialog dialog = new ProgressDialog(MyNameActivity.this); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
       .permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
     setContentView(R.layout.web_view_activity); 

     WebView wv; 
     wv = (WebView) findViewById(R.id.areaWebSolver); 
     wv.setWebViewClient(new WebViewClient() { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       view.loadUrl(url); 
       return true; 
      } 
      @Override 
     public void onPageFinished(WebView view, String url) {     
      if (dialog.isShowing()) { 
       dialog.dismiss(); 
      } 
     } 

     }); 
     dialog.setMessage("Loading..Please wait."); 
      dialog.setCanceledOnTouchOutside(false); 
      dialog.show(); 
     wv.loadUrl(url); 

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

    } 

不幸的是不起作用,應用程序崩潰與source not found ...如果我試圖刪除進度對話框的代碼活動的作品。怎麼了?我怎麼能解決這個問題?

回答

0

您可以設置ThreadPolicy不管是什麼原因之後方法setContentView(),你的進度對話框將永遠不會得到Context是低於你的聲明將不會加載活動場景都沒有。

private ProgressDialog dialog = new ProgressDialog(this); 

找到下面的僞代碼適用於我。

private ProgressDialog dialog; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.web_view_activity); 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
      .permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 

    dialog = new ProgressDialog(this); 
    dialog.setMessage("Loading..Please wait."); 
    dialog.setCanceledOnTouchOutside(false); 
    dialog.show(); 

    WebView wv = (WebView) findViewById(R.id.areaWebSolver); 
    wv.setWebViewClient(new WebViewClient() { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 
     @Override 
     public void onPageFinished(WebView view, String url) {     
      if (dialog.isShowing()) { 
       dialog.dismiss(); 
      } 
     } 
     ... 
    } 

不是由於您已連接及在IDE的類路徑中不可用其他一些外部庫中找到可能的來源。 READ HERE欲瞭解更多信息。

0

 private ProgressDialog dialog = new ProgressDialog(MyNameActivity.this); 
    //This will give you NullPointerException. 
    // You can use the activity context after you set the content to the activity.  

應該

 private ProgressDialog dialog; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
      .permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 
    setContentView(R.layout.web_view_activity); 
    dialog = new ProgressDialog(MyNameActivity.this); 
    .... 
    } 
0

你應該使用進度條在網頁視圖活動

// Just find 
progressBar = (ProgressBar) findViewById(R.id.progressBar1); 
//in onCreate Method of activity and than after 

    @Override 
      public void onPageFinished(WebView view, String url) { 
       // TODO Auto-generated method stub 
       super.onPageFinished(view, url); 
       progressBar.setVisibility(View.GONE); 
      } 
     //Progress bar gone in onFinished method of webview. 

XML:

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

    <WebView 
     android:id="@+id/webview01" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    </WebView> 

    <ProgressBar 
     android:id="@+id/progressBar1" 
     style="?android:attr/progressBarStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" /> 

</RelativeLayout> 
0

我認爲這會對你有所幫助。

private ProgressDialog dialog; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.web_view_activity); 
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
     .permitAll().build(); 
StrictMode.setThreadPolicy(policy); 



WebView wv = (WebView) findViewById(R.id.areaWebSolver); 
wv.setWebViewClient(new WebViewClient() { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    dialog = new ProgressDialog(this); 
    dialog.setMessage("Loading..Please wait."); 
    dialog.setCanceledOnTouchOutside(false); 

    if(!dialog.showing()){ 
     view.loadUrl(url); 
     dialog.show(); 
     return true; 
    } 

    } 
    @Override 
    public void onPageFinished(WebView view, String url) {     
     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 
    } 
    ... 
}