跳至主要内容

solution for AsyncTask.onPostExecute throws NullPointerException when fragment's view was destroyed in android



like title, there is the issue like example:
public class TestFragment extends Fragment {
    private boolean isDestroyed = false;
    private TextView mTextView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.simple_list_item_1, null);
        mTextView = (TextView) view.findViewById(R.id.text1);
        return view;
    }

    class FetchNewsTask extends AsyncTask {

        @Override
        protected void onPreExecute() {
            // show ProgressBar here
        }

        @Override
        protected String doInBackground(Void... params) {
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            mTextView.setText(result);
        }
    }
}
that is a very simple fragment in a program, but there have a bug if you do that.
AsyncTask well execute in another thread, but view is destroyed this thread maybe not executed, then it throws a nullpoint exception, so this is the issue.
I find a very simpleness solution for this issue, like this
public class TestFragment extends Fragment {
    private boolean isDestroyed = false;
    private TextView mTextView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.simple_list_item_1, null);
        mTextView = (TextView) view.findViewById(R.id.text1);
        return view;
    }

    class FetchNewsTask extends AsyncTask {

        @Override
        protected void onPreExecute() {
            // show ProgressBar here
        }

        @Override
        protected String doInBackground(Void... params) {
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // when task executed the text view text well change, 
            // but when this task return after this fragment changed, it must throws a NullPointerException
            if (isDestroyed) return;
            mTextView.setText(result);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // I set a flag here for this fragment's view was destroyed
        isDestroyed = true;
    }
}
now you app can not crash when you replace fragment that task has not return.

评论

  1. Thx sir, this code Insya Allah run fluently :)

    回复删除
  2. Thanks dude, I have been struggling for 8 days but to no avail. Cheers

    回复删除
    回复
    1. you can seed me email with your code. and I can help you if I can .

      删除
    2. jerry.jobs[at]qq.com replace [at] to '@'.

      删除
  3. Hi sir,i dont use fragments, instead i used sockets,but it is throwing this exception..can u help me

    回复删除
    回复
    1. show me your code. send to "jerry.jobs#qq.com" replace '#' to '@'.

      删除
  4. hi. good afternoon. im having a problem with my asynctask can you help me? thank you very much

    回复删除

发表评论

此博客中的热门博文

How to change ViewPager scroll animation duration and velocity

When you call change current selected view pager position you may call like this. // change current position default is viewPager.setCurrentItem(position, true); viewPager . setCurrentItem ( position ); There will show transaction animation for change position , but this animation is too fast. So I want change this animation. When I see source code I find this, like flow. where is the scroll animation and speed.     int duration = 0 ;     velocity = Math . abs ( velocity );     if ( velocity > 0 ) {         duration = 4 * Math . round ( 1000 * Math . abs ( distance / velocity ));     } else {         final float pageWidth = width * mAdapter . getPageWidth ( mCurItem );         final float pageDelta = ( float ) Math . abs ( dx ) / ( pageWidth + mPageMargin );         duration = ( int ) (( pageDelta + 1 ) * 100 ); ...

How to close SearchView in android with ActionbarSherlock or android ActionBar

How to close SearchView in android with ActionbarSherlock or android ActionBar When I use a search view for search something in android action bar; u must have create a menu for actionview( SearchView) @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.news_post_list, menu); mSearchView = (SearchView) menu.findItem(R.id.item_search).getActionView(); mSearchView.setQueryHint(getString(R.string.search_hint_news)); mSearchView.setOnQueryTextListener(this); super.onCreateOptionsMenu(menu, inflater); } and menu xml is : <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/item_search" android:icon="@drawable/ic_action_search" android:title="@string/search_hint" android:showAsAction="ifRoom" android:actionViewClass="com.actionbarsherlock.widget.SearchView"/> <item android:id...

how to open context menu for android listview item button

Like title, here is this problem in my activity like this: An more button in every row of listview item . When I click this more button then show activity context menu. So I try to click button, but open context menu is the Adaper's context menu. My  Adapter  is extends from BaseAdapter ,  BaseAdapter  has not context menu. My solution like example : <ImageButton android:id="@+id/more" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:background="#00000000" android:src="@drawable/btn_addressbook_more" android:onClick="onMoreClick"/> Add an event named  onMoreClick , when user click this button will do  onClick  event in  activity . // maybe you can registe...