2011-09-27 82 views
0

这件事让我发疯。我的应用程序加载webview并在初始加载时显示加载对话框。我希望每次单击链接或每次加载web视图时都会显示加载对话框。这没有发生。 ()onPageStarted()虽然onPageFinished()工作正常!本地没有使用!?Webview加载对话框

有人能看到什么错,我贴我所有的活动如下:

package com.jeh.myapp; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.Window; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

public class MyActivity extends Activity { 

    WebView myWebView; 
    public static final String TAG = "Main"; 
    public ProgressDialog progressBar; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //Remove title bar as we already have it in the web app 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     //Point to the content view defined in XML 
     setContentView(R.layout.main); 
     //Configure the webview setup in the xml layout 
     myWebView = (WebView) findViewById(R.id.webview); 
     WebSettings webSettings = myWebView.getSettings(); 
     //Yes, we want javascript, pls. 
     webSettings.setJavaScriptEnabled(true); 

     progressBar = ProgressDialog.show(MyActivity.this, "Diag Title", "Loading..."); 


     //Make sure links in the webview is handled by the webview and not sent to a full browser 
     myWebView.setWebViewClient(new WebViewClient() { 


      //this bit causes problems, if I add @Override here it says to remove, where as the current code marks onPageStarted yellow and says it's not used locally!? - yet onPageFinsihed() below works fine? 
      public void onPageStarted(WebView view, String url, Bitmap favicon) { 

       progressBar.show(); 
      } 


      public void onPageFinished(WebView view, String url) { 
       Log.i(TAG, "Finished loading URL: " +url); 
       if (progressBar.isShowing()) { 
        progressBar.dismiss(); 
       } 
      } 

     }); 
     //Load URL: 
     myWebView.loadUrl("http://www.google.com"); 



    } 
+0

嘿:它的@覆盖** **公共无效onPageStarted(...) –

+0

是啊,对不起试过太多,同样的问题。 – badger110

+0

这真的很奇怪,因为公共无效onPageFinished()工作正常... – badger110

回答

0

容易。 没有

void onPageStarted(WebView view, String url) 

方法在WebViewClient,只需使用:

@Override 
void onPageStarted(WebView view, String url, Bitmap favicon) 

这些错误是很容易的,不易发现。你应该使用@Override关键字,只要你知道你重写了一个方法(在你的情况下会标记一个错误)。

尝试修改源是这样的:

//Make sure links in the webview is handled by the webview and not sent to a full browser 
myWebView.setWebViewClient(new WebViewClient() { 
    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
     progressBar.show(); 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
     Log.i(TAG, "Finished loading URL: " +url); 
     if (progressBar.isShowing()) { 
      progressBar.dismiss(); 
     } 
    } 

}); 
+0

我刚刚尝试过,但它说的是完全一样的东西:( – badger110

+0

@ badger110嗯,很奇怪。你试过保存你的文件吗?你能更新你的代码吗? –

+0

嗨我更新了,如果我在onpagestarted()中添加一个@Override,它在红色下面加下划线,表示'必须覆盖或实现一个超类型方法'如果我快速修复并删除'@Override',它以黄色下划线告诉我它不是在本地使用的, t工作,当我在模拟器中测试 – badger110