我正在尝试创建一个ProgressDialog
,但我所有的努力都以 NPE 失败。我看过以前的帖子,例如AsyncTask 总是抛出 NullPointerException并尝试采用提供的解决方案但无济于事。为了测试,我创建了一个非常简单的应用程序,该应用程序带有一个按钮,应该启动ProgressDialog.
这里是Activity
:
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)
我认为上下文存在问题,但我不确定如何处理它。谢谢。