NullPointerException - Handler.removeCallbacks(java.lang.Runnable)

时间:2017-11-06 23:47:24

标签: android nullpointerexception handler runnable

我在控制台上的应用程序中收到了一些崩溃报告。我试着解决它,但我坚持了下来。我正在使用自定义进度对话框,我的问题就在于此。我该怎么办?我应该使用removeCallBack吗?

logcat

FlipProgressDialog fpd; // this is my custom progress dialog

@Override
public void onPause() {
    super.onPause();
    fpd.dismissAllowingStateLoss();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_top_feeds, container, false);
    this.mView = view;
    fpd = new FlipProgressDialog();  //and initialized here for escape the nullpointer exception but not worked


    fpd.setImageList(imageList); 
    fpd.setCanceledOnTouchOutside(false);

    // Handler handler = new Handler();
    Runnable myRunnable = new Runnable() {
        @Override
        public void run() {
            new Listeleme().execute();
        }
    };

    myRunnable.run();
    //handler.removeCallbacks(myRunnable);
    //I guess I need call the above but I couldn't apply properly
    return view;
}

private class Listeleme extends AsyncTask<Void,Void,Void>
{
    String URL="...";

    @Override
    protected  void onPreExecute()
    {
        super.onPreExecute();
        fpd.show(getActivity().getFragmentManager(),"");
    }
    @Override
    protected Void doInBackground(Void... params) {

      //...
        return null;
    }
    @Override
    protected void onPostExecute(Void avoid)
    {
        if (getActivity()!= null) {        
        }
        if (fpd.isVisible()) {
            fpd.dismissAllowingStateLoss();
        }
    }
}

这是245行 - FlipProgressDialog。的java

    @Override
public void onDestroyView() {
    Dialog dialog = getDialog();
    if (dialog != null && getRetainInstance()) {
        dialog.setDismissMessage(null);
    }
    handler.removeCallbacks(r);  // line 245
    super.onDestroyView();
}

1 个答案:

答案 0 :(得分:0)

显然,当您输入onPause()时,FlipProgressDialog的某些资源会被销毁,因此在所有内容被销毁后调用任何内容都会导致该异常。只需尝试在fpd.dismissAllowingStateLoss()

之前致电super.onPause()