-1

一切正常,除非它到达代码的最后一部分并显示“成功注册!” 然后标题中提到的错误出现在 registerDialog 消息部分中。

有什么我做错了吗?谁能帮我检查我的代码

非常感谢你。

该应用程序没有崩溃,尽管它只是退出到应用程序主页。如果我再次按下注册按钮,它将返回相同的错误。

私人无效setRegister(按钮b){b.setOnClickListener(新View.OnClickListener(){

        private AsyncTask<String, Void, String> task2;

        public void onClick(View v) {
            // TODO Auto-generated method stub
            registerDialog = new AlertDialog.Builder(Login.this).create();
            registerDialog.setTitle("Register Alert!");
            registerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Back", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            registerDialog.dismiss();
                        }
                    });

            task2 = new AsyncTask<String, Void, String>() {

                ProgressDialog dialog2;
                ArrayList<NameValuePair> postParameters2;
                String response2 = null;

                    @Override
                    protected void onPreExecute() {
                        postParameters2 = new ArrayList<NameValuePair>();
                        postParameters2.add(new BasicNameValuePair("usernamelog", rUsername.getText().toString()));
                        postParameters2.add(new BasicNameValuePair("passwordlog", rPassword.getText().toString()));
                        postParameters2.add(new BasicNameValuePair("rpasswordlog", rRpassword.getText().toString()));
                        postParameters2.add(new BasicNameValuePair("emaillog", rEmail.getText().toString()));
                        postParameters2.add(new BasicNameValuePair("phonenumlog", rPhoneNum.getText().toString()));
                        postParameters2.add(new BasicNameValuePair("doblog", rBirthday.getText().toString()));
                        postParameters2.add(new BasicNameValuePair("genderlog", rGender.getText().toString().toUpperCase(Locale.ENGLISH)));                 

                        dialog2 = new ProgressDialog(Login.this, ProgressDialog.STYLE_SPINNER);
                        dialog2.setMessage("Registering...");       

                        dialog2.show();
                    }

                    @Override
                    protected String doInBackground(String... params) {
                        try {
                            response2 = CustomHttpClient.executeHttpPost("http://whatstherex.info/checkR.php", postParameters2);

                            String res2 = response2.toString();

                            res2 = res2.replaceAll("null", "");

                            if (res2.equals("1")) {
                                res2 = "Successfully Registered!";
                                rAuth.setTextColor(Color.GREEN);
                                return res2;
                            } else {
                                res2 = res2.toString();
                                rAuth.setTextColor(Color.RED);
                                return res2;
                            }
                        } catch (Exception e) {
                            res2 = e.toString();
                            rAuth.setTextColor(Color.BLACK);
                            return res2;
                        }
                    }

                    @Override
                    protected void onPostExecute(String result2) {
                        if(!result2.equals("Successfully Registered!") && !result2.equals("Username have to be at least 5 characters") && !result2.equals("Password have to be at least 8 characters") && !result2.equals("Passwords does not match") && !result2.equals("Email field is empty") && !result2.equals("Email field is not valid") && !result2.equals("Phone number Field is empty") && !result2.equals("Phone number is not numeric") && !result2.equals("Phone number is not 8 digit") && !result2.equals("Birthday Field is empty") && !result2.equals("Birthday is not numeric") && !result2.equals("Birthday is not in DDMMYYYY format") && !result2.equals("Gender Field is empty") && !result2.equals("Gender Field is invalid") && !result2.equals("Gender not in the format M or F") && !result2.equals("Duplicate entry '"+rUsername.getText().toString()+"' for key 'username'") && !result2.equals("Duplicate entry '"+rEmail.getText().toString()+"' for key 'email'") && !result2.equals("Duplicate entry '"+rPhoneNum.getText().toString()+"' for key 'phoneNum'")){
                        rAuth.setText("Unknown Error!!!");
                        rAuth.setTextColor(Color.BLUE);
                        registerDialog.setMessage(result2);
                        registerDialog.show();
                        dialog2.dismiss();
                        }else if (result2.equals("Duplicate entry '"+rUsername.getText().toString()+"' for key 'username'")) {
                            rAuth.setText("Username already in used");
                            rAuth.setTextColor(Color.RED);
                            registerDialog.setMessage("Username already in used");
                            registerDialog.show();
                            dialog2.dismiss();
                        }else if (result2.equals("Duplicate entry '"+rEmail.getText().toString()+"' for key 'email'")) {
                            rAuth.setText("Email already in used");
                            rAuth.setTextColor(Color.RED);
                            registerDialog.setMessage("Email already in used");
                            registerDialog.show();
                            dialog2.dismiss();
                        }else if (result2.equals("Duplicate entry '"+rPhoneNum.getText().toString()+"' for key 'phoneNum'")){
                            rAuth.setText("Phone number already in used");
                            rAuth.setTextColor(Color.RED);
                            registerDialog.setMessage("Phone number already in used");
                            registerDialog.show();
                            dialog2.dismiss();
                        }else {
                            rAuth.setText("Successfully Registered!");
                            rAuth.setTextColor(Color.GREEN);
                            registerDialog.setMessage("Successfully Registered!");
                            registerDialog.show();
                            dialog2.dismiss();
                        }
                    }

            };
            task2.execute();                                    
        }
    });         
}
4

2 回答 2

4

我猜那rAuth是一个TextView. 这需要移至 ,onPostExecute()因为您无法UI更新doInBackground(). 只有onPostExecute(),onProgressUpdate()onPreExecute()UI线程上运行。

这是文档的链接

于 2013-01-10T14:09:58.333 回答
1

你做了一些 UI 工作doInBackground,例如这个调用

rAuth.setTextColor(Color.GREEN);

将此代码移动到onPostExecute方法中。

于 2013-01-10T14:10:57.160 回答