我正在嘗試創建一個ProgressDialog
,但是我的所有努力都無法通過NPE進行。我看過以前的帖子,如AsyncTask always throw NullPointerException,並試圖採用提供的解決方案,但無濟於事。爲了測試我創建了一個按鈕,一個非常簡單的應用程序,應該啓動ProgressDialog.
這裏是Activity
:AsyncTask中的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)
我想有一個與上下文中的問題,但我不知道如何處理它。謝謝。
我AsnycTask是不活動一個單獨的類文件。要創建ProgressDialog,我需要上下文。 – emwe
AsnyTask是我設置:public class ProgressAsyncTask extends AsyncTask我沒有參數傳遞給doInBackground方法 - 第一個無效。 Pregress被定義爲Short,因爲最大值是100,我不需要返回結果 - 再次是Void。 –
emwe
你要做的是將'ProgressAsync'放在'MainActivity'中,就像我上面顯示的那樣 – Jeeter