我创建了包含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)
正如我所见,构造函数运行时我有一个错误的上下文。是否有正确的方法以我的方式实现它?
答案 0 :(得分:0)
我通过覆盖主要活动的oncreate
方法来解决问题:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressDialog = new ProgressDialog(this);
}