I wish that when recreating the Activity, DialogProgress did not disappear with the status of AsyncTask being executed. To do this, I call AsyncTask in the fragment with setRetainInstance (true). ProgressDialog is created in the AsyncTask class in onPreExecute. When calling progress = new ProgressDialog (activity.get ()), an error appears

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference 

MainActivity code

 public class MainActivity extends AppCompatActivity { @BindView(R.id.counter)TextView counter; private MyAsyncTask2 task; private static final String MyFragment="MyFragment"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @OnClick(R.id.progressDialogButton) public void showProgressDialog(){ Log.d("happy","showProgressDialog()"); task=new MyAsyncTask2(MainActivity.this); task.execute(); } @OnClick(R.id.progressFragmentDialogButton) public void showFragmentProgressDialog(){ FragmentTaskWrapper myFragment=(FragmentTaskWrapper) getSupportFragmentManager().findFragmentByTag(MyFragment); if(myFragment==null){ myFragment=new FragmentTaskWrapper(); getSupportFragmentManager().beginTransaction().add(myFragment,MyFragment).commit(); } myFragment.startProgress(); } @Override protected void onPause() { super.onPause(); if(task!=null)task.cancel(false); } 

}

FragmentTaskWrapper code

 public class FragmentTaskWrapper extends Fragment { private MyAsyncTask2 task; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); Log.d("happy","FragmentTaskWrapper - onCreate"); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { Log.d("happy","FragmentTaskWrapper - onCreateView"); return super.onCreateView(inflater, container, savedInstanceState); } @Override public void onAttach(Context context) { Log.d("happy","FragmentTaskWrapper - onAttach"); super.onAttach(context); task.setActivity((MainActivity)getActivity()); } @Override public void onDetach() { super.onDetach(); task.setActivity(null); } public void startProgress(){ Log.d("happy","FragmentTaskWrapper - startProgress"); task=new MyAsyncTask2((MainActivity)getActivity()); task.execute(); } 

}

MyAsyncTask2 Code

 public class MyAsyncTask2 extends AsyncTask<Void,Integer,Void> { private WeakReference<MainActivity>activity; private ProgressDialog progress; public MyAsyncTask2(MainActivity activity) { Log.d("happy","MyAsyncTask2 - constructor"); this.activity = new WeakReference<MainActivity>(activity); } public void setActivity(MainActivity activity){ if (activity!=null){ this.activity=new WeakReference<MainActivity>(activity); } else{ this.activity=null; } } @Override protected void onPreExecute() { Log.d("happy","MyAsyncTask2 - onPreExecute"); if (activity!=null){ Log.d("happy","MyAsyncTask2 - onPreExecute IF activity != null"); progress=new ProgressDialog(activity.get()); progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progress.setTitle("Progress Dialog"); progress.setIndeterminate(false); progress.setProgress(0); progress.setMax(9); progress.setCancelable(false); progress.show(); } } @Override protected Void doInBackground(Void... params) { Log.d("happy","MyAsyncTask2 - doInBackground"); for (int i=0;i<10;i++){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } publishProgress(i); } return null; } @Override protected void onProgressUpdate(Integer... values) { Log.d("happy","MyAsyncTask2 - onProgressUpdate"); progress.setProgress(values[0]); } @Override protected void onPostExecute(Void aVoid) { Log.d("happy","MyAsyncTask2 - onPostExecute"); if (progress!=null){ progress.dismiss(); } } 

}

NPE in the class MyAsyncTask2 in the onPreExecute method

 progress=new ProgressDialog(activity.get()); 

Replacing activity.get (). GetApplicationContext () has no effect, the same NPE. Log messages in the matched fragment and AsyncTask methods are displayed in the following order:

  • FragmentTaskWrapper - startProgress
  • MyAsyncTask2 - constructor
  • MyAsyncTask2 - onPreExecute
  • MyAsyncTask2 - onPreExecute IF activity! = Null

Those. methods are not called at all

  • onCreate
  • onCreateView (checked too, even though there is no markup headless fragment, this method is not needed at all)
  • onAttach

and startProgress is immediately called. I don’t understand in the end how this fragment worked. Further, in AsyncTask, a separate check for null of the activity field takes place in the onPreExecute method, but then I cannot get the context from this field.

I read about fragments in more detail, yes, it is written that until the onCreate (and / or onCreateView?) Methods of this fragment are executed, this fragment does not exist.

But how then in AsyncTask'e Activity is not equal null? And the main thing is how to transfer the activations or context to AsyncTask so that the ProgressDialog can be created?

  • I struggled with a similar error that occurred when turning the screen with the progress bar progress. I did not overcome it and stupidly blocked the rotation of the screen until AsyncTask is AsyncTask - I will follow with interest :) - Barmaley
  • Here is the realization of what you want to do. Try it, maybe it will. - post_zeew
  • one
    It turned out @Barmaley youtu.be/cIMwRSseW_E github.com/Arvalon/ProgressDialog In general, it was necessary to add a couple of methods public void hideProgress () {progress.dismiss (); } public void showProgress () {progress.show (); } and still check ended with task or not - Arvalon

0