Armando Alberti
Armando Alberti

Reputation: 103

Progress Dialog crashes in async task

As the title says the app crashes when encounters the progress dialog create instruction. The context is passed to async class through constructor.

public class URLImageReader extends AsyncTask<URL, Void, Bitmap> {

    ImageView MyView = null;
    Activity context = null;

    private OnTaskComplete mlistener;

    public URLImageReader(Activity context,OnTaskComplete mlistener, ImageView view){
        this.context = context;
        this.mlistener = mlistener;
        MyView = view;
    }

    ProgressDialog dialog = new ProgressDialog(context);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog.setMessage("Loading...");
        dialog.show();
    }

    @Override
    protected Bitmap doInBackground(URL... params) {

        Bitmap image = null;

        try {
            URL url= params[0];
            image = BitmapFactory.decodeStream(url.openConnection().getInputStream());


        } catch (IOException e){
            e.printStackTrace();
        }

        return image;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        mlistener.callBackFunction(bitmap);
        MyView.setImageBitmap(bitmap);
        dialog.dismiss();
    }


}


03-04 07:59:18.531  14860-14860/youth_stories.com.youth_stories E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: youth_stories.com.youth_stories, PID: 14860
    java.lang.RuntimeException: Unable to start activity ComponentInfo{youth_stories.com.youth_stories/youth_stories.com.youth_stories.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
            at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:154)
            at android.app.AlertDialog.<init>(AlertDialog.java:109)
            at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
            at youth_stories.com.youth_stories.URLImageReader.<init>(URLImageReader.java:30)
            at youth_stories.com.youth_stories.Home.onCreate(Home.java:144)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-04 07:59:19.704  14860-14860/youth_stories.com.youth_stories I/Process﹕ Sending signal

Upvotes: 0

Views: 6561

Answers (2)

Armando Alberti
Armando Alberti

Reputation: 103

The problem was found in the passage of the ApplicationContext instead at Activity.

Upvotes: 1

Nagaraju V
Nagaraju V

Reputation: 2757

You have to pass Activity context to create dialog as

 Activity context = null;

 public URLDataReader(Activity context){
        this.context = context;
  }

Hope this will helps you.

Upvotes: 0

Related Questions