1

我有一个带有加载 url 的 WeView 的 Activity,我想在网站加载期间显示一个小等待对话框,所以我尝试了这个:

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...如果我尝试删除进度对话框代码,则活动可以正常工作。怎么了?我该如何解决这个问题?

4

4 回答 4

0

您应该在 webview 活动中使用进度条

// 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>
于 2013-07-01T05:23:57.270 回答
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);
    .... 
    }
于 2013-07-01T04:42:09.830 回答
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 的类路径中不可用。阅读此处了解更多信息。

于 2013-07-01T04:12:41.810 回答
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();
       }
    }
    ...
}
于 2013-07-01T10:56:51.953 回答