【问题标题】:Progress Dialog crashes in async task进度对话框在异步任务中崩溃
【发布时间】:2015-03-03 10:15:32
【问题描述】:

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

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

【问题讨论】:

  • 你能告诉我如何得到这个吗? ProgressDialog 对话框 = new ProgressDialog(getApplicationContext());这是在家上课吧?
  • 你在活动中创建了这个异步任务吗?
  • 您在哪里关闭进度对话框?你的 postExecute 方法在哪里?
  • @ArmandoAlberti 使用 Activity 的上下文,而不是 Application 的上下文。

标签: android android-asynctask progressdialog android-context


【解决方案1】:

问题是在ApplicationContext而不是Activity的段落中发现的。

【讨论】:

  • ArmandoAlberti:请提供更多信息问题是什么/您是如何解决的。 @PeterPeiGuo:他回答了自己的问题!
【解决方案2】:

你必须传递 Activity 上下文来创建对话框

 Activity context = null;

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多