-1

I can not modify UI onPostExecute in DialogFragment

I have already read all questions about this. And this's not the same case. Believe me.

I know an AsyncTask doInBackground function has his own thread, and you must touch the view in onPreExecute and onPostExecute. And with all these things, the app is still crashing.

The exception I get is:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at .onPostExecute(CustomDialogFragment.java:310)     
            at android.os.AsyncTask.finish(AsyncTask.java:631)                

Can't touch views from onPostExecute's AsyncTask

Community
  • 1
  • 1
Kamil Nękanowicz
  • 6,254
  • 7
  • 34
  • 51

3 Answers3

0

instead of activity.runOnUiThread use getActivity().runonUiThread

Kapil Vats
  • 5,485
  • 1
  • 27
  • 30
0

As @Raghunandan correctly mentioned, onPostExecute is invoked on the UI Thread so runOnUiThread is not necessary.

Change your AsyncTask to get Context in its constructor:

private class DownloadFilesTask extends AsyncTask<String,String,String> {

    private Context context;
    public DownloadFilesTask(Context context) {
            super();
            this.context = context;
    }

and use it like this:

@Override
protected void onPostExecute(String result) {
        super.onPostExecute(result);

        Toast.makeText(context, 
                      "why onPostExecute not work", Toast.LENGTH_SHORT).show();

}

Don't forget to instantiate the AsyncTask with the context:

final DownloadFilesTask downloadTask = new DownloadFilesTask(getActivity());
Sam R.
  • 16,027
  • 12
  • 69
  • 122
0

The problem was much deeper than it seems.

The real problem was because I use Toast.show() and dimiss() dialog from onPostExecute. Small time delay(100ms) solves the problem

Kamil Nękanowicz
  • 6,254
  • 7
  • 34
  • 51