我做了一个进度对话框,我想在同步过程中出现。启动同步的代码是
if(check.isDeviceConnected())
{
Sincro = new Synk(this);
Sincro.start();
ProgressDialog myDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");
}
我尝试将myDialog.dismiss();
放在大括号后面,但是应用程序崩溃时,Java Null Pointer Exception
排在myDialog.dismiss()
的行。为什么?如何让它在同步后消失?
答案 0 :(得分:1)
//Create a variable for ProgressDialog
ProgressDialog myDialog = null;
if(check.isDeviceConnected())
{
Sincro = new Synk(this);
Sincro.start();
//initialize your dialog
myDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");
}
//use this where you want to dismiss
myDialog.dismiss();
如果您的问题得到解决,请告诉我。
答案 1 :(得分:0)
尝试 -
if(myDialog!=null)
myDialog.dismiss();
答案 2 :(得分:0)
您将获得NullPointerException
(因为您正在重新定义变量),因为myDialog
的范围仅在条件和您已全局声明的变量被指定为null之前。而是尝试
if(check.isDeviceConnected())
{
Sincro = new Synk(this);
Sincro.start();
myDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");
}
myDailog.dismiss();// where ever you wish to dismiss
答案 3 :(得分:0)
ProgressDialog myDialog;
if(check.isDeviceConnected())
{
Sincro = new Synk(this);
Sincro.start();
myDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");
}
inside your sincro class create an onFinish like function:
onSyncComplete(){
if(myDialog!=null){
myDialog.dismiss();
}
}