-2

I made a progress dialog that I would like appeared during synchronization. The code that starts the synchronization is this

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

I tried to put myDialog.dismiss(); after the brace, but the app crashes with a Java Null Pointer Exception in line where it is myDialog.dismiss(). Why? How do I make it disappear after Sync?

4

4 回答 4

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();  

让我知道你的问题是否解决了..

于 2013-07-29T06:15:14.407 回答
0

尝试-

 if(myDialog!=null)
   myDialog.dismiss();
于 2013-07-29T06:15:33.160 回答
0

您正在获得NullPointerException(因为您再次重新定义变量),因为范围myDialog仅在 if 条件和您已全局声明的变量被分配为 null 之前。而是尝试

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

myDailog.dismiss();// where ever you wish to dismiss
于 2013-07-29T06:16:00.303 回答
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();
    }

 }
于 2013-07-29T06:19:37.927 回答