只有创建视图层次结构的原始线程才能触及其视图ERROR

时间:2013-01-10 14:04:40

标签: android view android-asynctask progressdialog

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

我做错了什么?任何人都可以帮我查看我的代码

非常感谢。

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

  

private void setRegister(Button b){           b.setOnClickListener(new 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();                                    
        }
    });         
}

2 个答案:

答案 0 :(得分:4)

我猜测rAuthTextView。由于您无法更新onPostExecute()中的UI元素,因此需要将其移至doInBackground()。只有onPostExecute()onProgressUpdate()onPreExecute()UI主题上运行。

Here is Link to Docs

答案 1 :(得分:1)

您在doInBackground中执行了一些UI工作,例如此次调用

rAuth.setTextColor(Color.GREEN);

将此代码移至onPostExecute方法。