关闭 ProgressDialog 时应用程序崩溃

App Crashes when Dismissing ProgressDialog

我在我的 WebView 应用程序中使用 ProgressDialog。 在 onPageStarted 中,我显示了对话框,但是在 onPageFinished 中使用 pd.dismiss 时,应用程序崩溃了。

web.setWebViewClient(new WebViewClient() {

    //Opens all the Clicked Links in App itself.. (No Opera/Browser/Chrome/Uc popup)
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    //Shows a Dialog when there is an Error
    @Override
    public void onReceivedError(WebView view, int errorcode,String description, String fallingUrl) {
        Toast.makeText(getActivity(),"NetWork Error", Toast.LENGTH_SHORT).show();
    }

    //Showing ProgressBar when Page is Loading
    @Override
    public void onPageStarted(WebView view,String url , Bitmap favicon){
        ProgressDialog pd = new ProgressDialog(getActivity());
        pd.setMessage("Loading...");
        pd.show();

    }

    //Hiding ProgressBar when Loading Finished
    public void onPageFinished(WebView view,String Url){
            pd.dismiss();
    }
});

ProgressDialog pd = new ProgressDialog(getActivity()); web.setWebViewClient(新的 WebViewClient() {

                 //Opens all the Clicked Links in App itself.. (No Opera/Browser/Chrome/Uc popup)
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }

                //Shows a Dialog when there is an Error
                @Override
                public void onReceivedError(WebView view, int errorcode,String description, String fallingUrl) {
                    Toast.makeText(getActivity(),"NetWork Error", Toast.LENGTH_SHORT).show();
                }

                //Showing ProgressBar when Page is Loading
                @Override
                public void onPageStarted(WebView view,String url , Bitmap favicon){

                    pd.setMessage("Loading...");
                    pd.show();

                }

                //Hiding ProgressBar when Loading Finished
                public void onPageFinished(WebView view,String Url){
                        pd.dismiss();
                }
            });

对我有用。

试试这个:

//Declare it globally
 LinearLayout progressLayout;

在onCreate()中:

 progressLayout = (LinearLayout) findViewById(R.id.ll_refreshing_progress);
 wv_tandc.setWebViewClient(new MyWebViewClient());
 wv_tandc.loadUrl(getString(R.string.terms_and_conditions_url));

实现 WebViewClient :

  private class MyWebViewClient extends WebViewClient {

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

            super.onPageStarted(view, url, favicon);
            progressLayout.setVisibility(View.VISIBLE);
        }


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            System.out.println("on finish");
            progressLayout.setVisibility(View.GONE);

        }
    }

在您定义 WebView 的 xml、 中编写以下代码:

<LinearLayout
        android:id="@+id/ll_refreshing_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone"
        android:padding="5dp"
        android:orientation="vertical">

        <ProgressBar
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sf_refreshing"/>

    </LinearLayout>

您不需要使用 progressdialog,而是可以使用自定义布局。

它发生在 nullPointerException 上。您需要将 ProgressDialog 作为 class 的实例文件并在创建时对其进行初始化。像下面这样:

private ProgressDialog pd;

@override
public void onCreate () {
    pd = new ProgressDialog(getActivity());
    pd.setMessage("Loading...");
}

现在,当您需要显示时,只需在该 pd progressDialog 上调用 show() 方法,您可以随时通过调用 dismiss() 方法将其关闭。

基于此,我假设pd是一个成员变量。

//Hiding ProgressBar when Loading Finished
public void onPageFinished(WebView view,String Url){
        pd.dismiss();
}

但是,您似乎认为这是在分配相同的 pd 变量,但事实并非如此。参见 variable shadowing

//Showing ProgressBar when Page is Loading
@Override
public void onPageStarted(WebView view,String url , Bitmap favicon){
    ProgressDialog pd = new ProgressDialog(getActivity());
    pd.setMessage("Loading...");
    pd.show();

}

ProgressDialog pd = 中删除 ProgressDialog,所以只有 pd = new ...

声明ProgressDialog对象为成员变量

public void onPageFinished(WebView view,String Url){
          if(pd ! =null)                  
             pd.dismiss();       
      }

改变一下

@Override
                public void onPageStarted(WebView view,String url , Bitmap favicon){
                    pd = new ProgressDialog(getActivity());
                    pd.setMessage("Loading...");
                    pd.show();

                }

                //Hiding ProgressBar when Loading Finished
                public void onPageFinished(WebView view,String Url){
                        pd.dismiss();
                }

并将对话框对象声明为

private ProgressDialog pd;