【问题标题】:Loading Dialog Box for WebviewWebview 的加载对话框
【发布时间】:2011-09-27 11:48:34
【问题描述】:

这件事让我发疯。我的应用程序加载 webview 并在初始加载时显示加载对话框。我希望每次单击链接或每次加载 webview 时都会出现加载对话框。这不会发生。

Eclipse 告诉我 onPageStarted() 没有在本地使用,虽然 onPageFinished() 工作正常!?

任何人都可以看到出了什么问题,我已将我的所有活动粘贴在下面:

   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");



    }

【问题讨论】:

  • 嘿:这是@Override public void onPageStarted(...)
  • 是的,抱歉也试过了,同样的问题。
  • 这真的很奇怪,因为 public void onPageFinished() 工作正常......
  • 您是否尝试保留@Override 并添加 import android.graphics.Bitmap; ? (在您的代码中看不到)
  • 干!就是这样!我非常依赖 eclipse 来告诉我缺少的导入,当它没有给我提示时我忽略了它。非常感谢你!

标签: java android


【解决方案1】:

简单。 没有

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();
         }
     }

});

【讨论】:

  • @badger110 嗯,很奇怪。您是否尝试保存文件?你能根据你的问题更新你的代码吗?
  • 嗨,我已经更新了,如果我在 onpagestarted() 中添加一个@Override,它会用红色下划线表示“必须覆盖或实现超类型方法”如果我快速修复并删除“@Override”它黄色下划线告诉我它没有在本地使用,因此在我在模拟器中测试时不起作用。
  • @badger110 确保位图在您的导入中: import android.graphics.Bitmap;否则无法识别方法签名,但请继续使用@Override,否则您的错误仍然存​​在但已被屏蔽。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-23
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多