进度对话框未显示

时间:2012-06-28 11:59:09

标签: android

我是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。如何在我的案例中显示对话框。 感谢

1 个答案:

答案 0 :(得分:0)

如果你想要与getBaseContext()或与之相关的对话,而不是使用

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

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

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

,另一种方式是

如上所述

  ProgressDialog dialog = new ProgressDialog(YourRecentActivity.this);