Only the original thread that created a view hierarchy can touch its views ERROR

烂漫一生 提交于 2019-12-20 07:39:50

问题


Everything works except when it reaches till the last part of the code with "Successfully Registered!" then the error as mentioned in the title appears inside the registerDialog message part.

Anything im doing wrongly? can anyone help me check up my code

Thank you very much.

The app didn't crash though it just exit back to the app main page. and if i press the register button again it will return with the same error.

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();                                    
        }
    });         
}

回答1:


I'm guessing that rAuth is a TextView. This needs to be moved to the onPostExecute() as you can't update UI elements in the doInBackground(). Only the onPostExecute(), onProgressUpdate() and onPreExecute() run on the UI thread.

Here is Link to Docs




回答2:


You do some UI work in your doInBackground, for example this call

rAuth.setTextColor(Color.GREEN);

Move this code to the onPostExecute method.



来源:https://stackoverflow.com/questions/14259708/only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-views-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!