如何在同步后停止进度对话框?

时间:2013-07-29 06:11:22

标签: java android

我做了一个进度对话框,我想在同步过程中出现。启动同步的代码是

if(check.isDeviceConnected())
{
    Sincro = new Synk(this);
    Sincro.start();
    ProgressDialog myDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");
}

我尝试将myDialog.dismiss();放在大括号后面,但是应用程序崩溃时,Java Null Pointer Exception排在myDialog.dismiss()的行。为什么?如何让它在同步后消失?

4 个答案:

答案 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();
    }

 }