2012-06-28 40 views
0

我是android新手。我正在创建一个登录机制,当您单击登录按钮时,它会调用一个Web服务,该服务会检查您是否已通过身份验证。同时我希望只要结果不是从服务中返回,那么应该显示进度条,当结果出现时,进度对话框应该消失,并且我导航到另一个您成功登录的活动,并显示一条消息,表示抱歉错误用户名和密码。我也想显示取消按钮,所以如果认证需要很长时间,用户可以选择关闭对话框。我试过这个,但我得到错误。进度对话框不显示

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.login); 

    Button btn_logIn = (Button)findViewById(R.id.btn_signIn); 
    btn_logIn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View view) { 

      ProgressDialog dialog = new ProgressDialog(getBaseContext()); 
      dialog.setMessage("Please Wait. Your authentication is in progress"); 
      dialog.setButton("Cancel", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) {       
        dialog.dismiss();      
       } 
      }); //end of anonymous class 

      dialog.show();    
     }   
    }); //end of anonymous class  
} //end of onCreate() 

当我点击登录按钮,然后我得到消息强制关闭应用。我没有使用onCreateDialog(int id)方法,因为我读了关于developer.android.com是

Opening a progress dialog can be as simple as calling ProgressDialog.show(). For 
example, the progress dialog shown to the right can be easily achieved without 
managing the dialog through the onCreateDialog(int) callback, as shown here: 

ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", 
        "Loading. Please wait...", true); 

看到它是说没有必要使用onCreateDialog()的进度对话框。 without managing the dialog through the onCreateDialog(int) callback。我如何在我的情况下显示对话框。 感谢

+0

尝试绕着弯,而不是'新ProgressDialog(getBaseContext());' –

+0

谢谢:)它worked.Why它现在与getBaseContext工作()?另外我想要问的取消按钮点击可以打电话dismiss()或者我应该打电话取消()?谢谢 – Basit

回答

0

如果u希望UR有getBaseContext(对话),或与此相关的比使用任何

ProgressDialog dialog = new ProgressDialog(view.getBaseContext()); 

ProgressDialog dialog = new ProgressDialog(view.getContext()); 

ProgressDialog dialog = new ProgressDialog(view.getApplicationContext()); 

而另一种方式是

通过使用`新ProgressDialog(YOUR_CURRENT_ACTIVITY.this)以上

ProgressDialog dialog = new ProgressDialog(YourRecentActivity.this);