2013-07-29 89 views
0

我正在尝试创建一个ProgressDialog,但是我的所有努力都无法通过NPE进行。我看过以前的帖子,如AsyncTask always throw NullPointerException,并试图采用提供的解决方案,但无济于事。为了测试我创建了一个按钮,一个非常简单的应用程序,应该启动ProgressDialog. 这里是ActivityAsyncTask中的Progressdialog导致NullPointerException

public class MainActivity extends Activity { 

    Context context = this; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button startButton = (Button) findViewById(R.id.button1); 

     startButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       new ProgressAsyncTask(context).execute(); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

而这里的AsyncTask类文件(这是一个单独的文件完全):

public class ProgressAsyncTask extends AsyncTask<Void, Short, Void>{ 

    private Context context_2; 
    ProgressDialog dialog = new ProgressDialog(context_2); 
    short max=100; 

    public ProgressAsyncTask(Context context_1) { 
     // TODO Auto-generated constructor stub 
     context_2=context_1; 
    } 

    @Override 
    protected void onPreExecute() { 
     dialog.setTitle("Test"); 
     dialog.setMax(max); 
     dialog.setCancelable(false); 
     dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     dialog.setMessage("Bitte warten"); 
     dialog.show(); 

    } 

    @Override 
    protected Void doInBackground(Void... unused) { 
     // TODO Auto-generated method stub 
     for (short i=1; i<=max; i++) { 
      publishProgress(i); 
      try { 
       Thread.sleep(500); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Short...shorts) { 
     dialog.incrementProgressBy(shorts[0]); 

    } 

    protected void onPostExecute() { 
     dialog.dismiss(); 
    } 

} 

这里是我的logcat输出:

07-29 18:53:31.373: E/Trace(14666): error opening trace file: No such file or directory (2) 
07-29 18:53:34.212: E/AndroidRuntime(14666): FATAL EXCEPTION: main 
07-29 18:53:34.212: E/AndroidRuntime(14666): java.lang.NullPointerException 
07-29 18:53:34.212: E/AndroidRuntime(14666): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at android.app.AlertDialog.<init>(AlertDialog.java:98) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at android.app.ProgressDialog.<init>(ProgressDialog.java:77) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at de.emwe.progressdialog.ProgressAsyncTask.<init>(ProgressAsyncTask.java:21) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at de.emwe.progressdialog.MainActivity$1.onClick(MainActivity.java:26) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at android.view.View.performClick(View.java:4204) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at android.view.View$PerformClick.run(View.java:17355) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at android.os.Handler.handleCallback(Handler.java:725) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at android.os.Handler.dispatchMessage(Handler.java:92) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at android.os.Looper.loop(Looper.java:137) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at android.app.ActivityThread.main(ActivityThread.java:5041) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at java.lang.reflect.Method.invokeNative(Native Method) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at java.lang.reflect.Method.invoke(Method.java:511) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
07-29 18:53:34.212: E/AndroidRuntime(14666): at dalvik.system.NativeStart.main(Native Method) 

我想有一个与上下文中的问题,但我不知道如何处理它。谢谢。

回答

1

根据我的经验,只要它在同一个类中,您不需要调用AsyncTask的构造函数AsyncTask,您可以说new ProgressAsyncTask().execute();您不需要构造函数。

现在,最大限度地减少对Context问题,您可以执行以下操作:

public class MainActivity extends Activity { 
    ... 
    ... 
    ... 
    private class ProgressAsync extends AsyncTask<Params, Progress, Result> { //Fill these in with classes 
     ... 
    } 
} 

而且你的电话线将是:

public void onClick(View v) { 
     // TODO Auto-generated method stub 
     new ProgressAsyncTask().execute(/*An instance of type Params*/); 
} 

编辑

现在,我看到了你出错的地方: This line:

ProgressDialog dialog = new ProgressDialog(context_2); 

是问题所在。您正在初始化对话框BEFORE context_2已初始化,所以如果您尝试使用它,将会抛出NullPointerException

你应该做的是:

ProgressDialog dialog; 
public ProgressAsyncTask(Context context_1) { 
    // TODO Auto-generated constructor stub 
    context_2=context_1; //NOT NEEDED! See below. 
    dialog = new ProgressDialog(context_1); //You can just eliminate context_2 and use context_1; 
} 
+0

我AsnycTask是不活动一个单独的类文件。要创建ProgressDialog,我需要上下文。 – emwe

+0

AsnyTask是我设置:public class ProgressAsyncTask extends AsyncTask 我没有参数传递给doInBackground方法 - 第一个无效。 Pregress被定义为Short,因为最大值是100,我不需要返回结果 - 再次是Void。 – emwe

+0

你要做的是将'ProgressAsync'放在'MainActivity'中,就像我上面显示的那样 – Jeeter

相关问题