0

我是安卓新手。我正在建立一种登录机制,当您单击“登录”按钮时,它会调用 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. 在我的情况下如何显示对话框。谢谢

4

1 回答 1

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);
于 2012-06-28T12:19:47.770 回答