解雇ProgressDialog时App崩溃

时间:2016-09-16 10:36:14

标签: android progressdialog

我在str(snapshot['SnapshotId'])应用中使用ProgressDialog。 在WebView中,我显示了对话框,但在onPageStarted中使用pd.dismiss时,应用程序崩溃了。

onPageFinished

7 个答案:

答案 0 :(得分:2)

它发生在nullPointerException。您需要将ProgressDialog作为该类的实例字段并将其初始化为onCreate。如下所示:

private ProgressDialog pd;

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

现在当你需要在那个pd progressDialog上显示只调用show()方法时,你可以随时通过调用dismiss()方法来解散。

答案 1 :(得分:1)

ProgressDialog pd = new ProgressDialog(getActivity()); 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){

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

                }

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

它对我有用。

答案 2 :(得分:0)

试试这个:

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

        }
    }

在xml中编写以下代码,在哪里定义了WebView:

<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,而是可以使用自定义布局。

答案 3 :(得分:0)

基于此,我假设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移除ProgressDialog pd =,只需pd = new ...

答案 4 :(得分:0)

ProgressDialog个对象声明为成员变量

答案 5 :(得分:0)

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

答案 6 :(得分:0)

只需更改

@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;