android在构造函数中创建ProgressDialog

时间:2017-02-14 12:05:04

标签: java android

我创建了包含ProgressDialog及其方法的Activity。

public class ProgressActivity extends AppCompatActivity implements Thread.UncaughtExceptionHandler {

    public final ProgressDialog progressDialog;

    public void dismissPD() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    public void setPDMessage(String msg) {
        progressDialog.setMessage(msg);
    }

    public void showPD(String msg) {
        ProgressDialog.show(this, "", msg);
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        ex.printStackTrace();
    }

    public ProgressActivity() {
        progressDialog = new ProgressDialog(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissPD();
    }

}

现在我从中扩展了我的新活动,但我在NullpointerException上的构造函数中得到progressDialog = new ProgressDialog(this) 正如我所见,构造函数运行时我有一个错误的上下文。是否有正确的方法以我的方式实现它?

1 个答案:

答案 0 :(得分:0)

我通过覆盖主要活动的oncreate方法来解决问题:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    progressDialog = new ProgressDialog(this);
}