0

我正在尝试创建一个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)

我认为上下文存在问题,但我不确定如何处理它。谢谢。

4

1 回答 1

1

根据我的经验,只要 a 在同一个类中,您就不需要调用AsyncTask构造函数Context,您可以说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*/);
}

编辑

现在,我看到了你出错的地方:这一行:

ProgressDialog dialog = new ProgressDialog(context_2);

是问题所在。您正在初始化对话框之前context_2初始化,因此NullPointerException如果您尝试对其进行任何操作,它将抛出 a 。

你应该做的是:

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;
}
于 2013-07-29T19:41:22.597 回答