2015-03-03 110 views
0

正如標題所示,當遇到進度對話框創建指令時,應用程序崩潰。上下文通過構造函數傳遞給異步類。異步任務中的進度對話框崩潰

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 
+0

什麼是錯誤? – Fahim 2015-03-03 10:15:59

+0

你能告訴我如何得到這個? ProgressDialog dialog = new ProgressDialog(getApplicationContext());這是在家庭課上吧? – 2015-03-03 10:19:17

+0

粘貼你的logcat – virendrao 2015-03-03 10:19:38

回答

0

你要通過活動上下文來創建對話框,

Activity context = null; 

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

希望這將幫助你。

1

問題發現在ApplicationContext而不是Activity

+3

ArmandoAlberti:請提供更多的信息是什麼問題是/你如何解決它。彼得·皮果:他回答了他自己的問題! – 2015-03-04 06:54:57

+0

您可以通過單擊刻度標記來「接受」自己的答案。這標誌着問題的解決。 – 2015-05-30 21:28:58