进度对话框在异步任务中崩溃

时间:2015-03-03 10:15:32

标签: android android-asynctask progressdialog android-context

正如标题所说,当遇到进度对话框创建指令时应用程序崩溃。上下文通过构造函数传递给异步类。

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

2 个答案:

答案 0 :(得分:1)

ApplicationContext代替Activity时发现了问题。

答案 1 :(得分:0)

您必须传递活动上下文以

创建对话框
 Activity context = null;

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

希望这会对你有所帮助。