Usi Usi
Usi Usi

Reputation: 2997

Android strange error reported on the onPreExecute method

Today my G. Analytics account reports me an error on this line of code

private class GetSubscriptionListTask extends AsyncTask<Void, Void, Void> {

    boolean onlyUnread=true;

    public GetSubscriptionListTask(boolean onlyUnread) {
        super();
        this.onlyUnread=onlyUnread;
    }

    ProgressDialog progress;

    @Override
    protected void onPreExecute() {
          //Show progress Dialog here
          super.onPreExecute();

          // create ProgressDialog here ...
          progress = new ProgressDialog(getActivity()); // <-- That's the line
          progress.setMessage("Downloading Subscriptions");
          // set other progressbar attributes

          progress.setCancelable (false);
          progress.setIndeterminate (true);
          progress.show();

    }
}

This is the line

progress = new ProgressDialog(getActivity()); // <-- That's the line

and this is the error report

NullPointerException (@SubscriptionsListFragment$GetSubscriptionListTask:onPreExecute:415) {main}

what does it mean? The nullexception is about the ProgressDialog or the getActivity()?

*UPDATE** This error happens only one time on over 100 sessions.

Upvotes: 0

Views: 292

Answers (3)

Sunil Kumar
Sunil Kumar

Reputation: 7082

You can use the getActivity() if your class extends the FragmentActivity. else you try the yourActivty.this if your Class extends Activity

or you get the Activty reference in Fragment class call onAttach

Activity mActivity=null
@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

Upvotes: 1

Zoubiock
Zoubiock

Reputation: 1165

Sometimes, Fragments are detached from your activity and getActivity() returns null.

You can see this here: http://developer.android.com/guide/components/fragments.html

Caution: If you need a Context object within your Fragment, you can call getActivity(). However, be careful to call getActivity() only when the fragment is attached to an activity. When the fragment is not yet attached, or was detached during the end of its lifecycle, getActivity() will return null.

That's why you get a NPE here.

Upvotes: 1

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

// try this way
private class GetSubscriptionListTask extends AsyncTask<Void, Void, Void> {

        boolean onlyUnread=true;
        Context context;

        public GetSubscriptionListTask(boolean onlyUnread,Context context) {
            super();
            this.onlyUnread=onlyUnread;
            this.context=context;
        }

        ProgressDialog progress;

        @Override
        protected void onPreExecute() {
            //Show progress Dialog here
            super.onPreExecute();

            // create ProgressDialog here ...
            progress = new ProgressDialog(context); <-- That's the line
            progress.setMessage("Downloading Subscriptions");
            // set other progressbar attributes

            progress.setCancelable (false);
            progress.setIndeterminate (true);
            progress.show();

        }

Upvotes: 0

Related Questions