可能重复:
android-原因:android.view.ViewRootImpl$CalledFromWrongThreadException
我正在使用此示例http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/当我在注册表中注册了我的详细信息时,它将数据保存在数据库中,但应用程序崩溃,它给出了以下错误
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
我的注册码低于
Register.java
import org.json.JSONException;
import org.json.JSONObject;
import com.example.petencare.library.JSONParser;
import com.example.petencare.library.DatabaseHandler;
import com.example.petencare.library.UserFunctions;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* Handles the user registration activity.
*
*/
public class Register extends Activity {
JSONParser jsonParser = new JSONParser();
private EditText newUsername;
private EditText newPassword;
private EditText newConfiPass;
private EditText newName;
private EditText newPhone;
private TextView Alreadyuser;
private Button registerButton;
private TextView registerErrorMsg;
private ProgressDialog pDialog;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_EMAIL = "email";
private static String KEY_NAME = "name";
private static String KEY_MOBILE = "mobile";
private static String KEY_CREATED_AT = "created_at";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
newUsername = (EditText) findViewById(R.id.reg_email);
newPassword = (EditText) findViewById(R.id.reg_pass);
newConfiPass = (EditText) findViewById(R.id.reg_passre);
newName =(EditText)findViewById(R.id.reg_name);
newPhone =(EditText)findViewById(R.id.reg_mobile);
registerButton = (Button) findViewById(R.id.btnRegister);
Alreadyuser = (TextView)findViewById(R.id.link_to_login);
registerErrorMsg= (TextView)findViewById(R.id.register_error);
Alreadyuser.setOnClickListener(new TextView.OnClickListener(){
public void onClick (View v){
Intent i = new Intent(v.getContext(), Login.class);
startActivity(i);
// Close Registration View
finish();
}
});
registerButton.setOnClickListener(new View.OnClickListener() {
public void onClick (View view){
new Registeruser().execute();
}
});
}
class Registeruser extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Register.this);
pDialog.setMessage("Registering. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
//Get user details.
String email = newUsername.getText().toString();
String password = newPassword.getText().toString();
String confirmpassword = newConfiPass.getText().toString();
String name= newName.getText().toString();
String mobile = newPhone.getText().toString();
//Check if all fields have been completed.
if (email.equals("") || password.equals("") || name.equals("") || mobile.equals("")){
Toast.makeText(getApplicationContext(),
"Please ensure all fields have been completed.",
Toast.LENGTH_SHORT).show();
return null;
}
//Check password match.
if (!password.equals(confirmpassword)) {
Toast.makeText(getApplicationContext(),
"The password does not match.",
Toast.LENGTH_SHORT).show();
newPassword.setText("");
newConfiPass.setText("");
return null;
}
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.registerUser(email, password, name, mobile);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully registred
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_EMAIL), json_user.getString(KEY_NAME), json.getString(KEY_UID), json_user.getString(KEY_MOBILE), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard1 = new Intent(getApplicationContext(), MainActivity.class);
// Close all views before launching Dashboard
dashboard1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard1);
// Close Registration Screen
finish();
}else{
// Error in registration
registerErrorMsg.setText("User already existed");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url){
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
我使用JSON解析来发送数据,我有一个userfunction.java类,代码在下面
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.content.Context;
public class UserFunctions {
private JSONParser jsonParser;
// Testing in localhost using wamp or xampp
// use http://10.0.2.2/ to connect to your localhost ie http://localhost/
private static String loginURL = "http://www.mywebsite.com/android_login_api/";
private static String registerURL = "http://www.mywebsite.com/android_login_api/";
private static String login_tag = "login";
private static String register_tag = "register";
// constructor
public UserFunctions(){
jsonParser = new JSONParser();
}
/**
* function make Login Request
* @param email
* @param password
* */
public JSONObject loginUser(String email, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
// return json
// Log.e("JSON", json.toString());
return json;
}
/**
* function make Login Request
* @param name
* @param email
* @param password
* */
public JSONObject registerUser(String email, String password, String name, String mobile){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", register_tag));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("mobile", mobile));
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
// return json
return json;
}
/**
* Function get Login status
* */
public boolean isUserLoggedIn(Context context){
DatabaseHandler db = new DatabaseHandler(context);
int count = db.getRowCount();
if(count > 0){
// user logged in
return true;
}
return false;
}
/**
* Function to logout user
* Reset Database
* */
public boolean logoutUser(Context context){
DatabaseHandler db = new DatabaseHandler(context);
db.resetTables();
return true;
}
}
请帮我提前感谢
我收到错误
引起原因:android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能接触其视图。
意味着您正试图从AsyncTask的doInBackground
访问UI元素。不可能从非Ui线程访问Ui元素或生成CCD_ 2。
解决方案是在启动AsyncTask之前移动所有类似EditText验证的代码,并使用AsyncTask的onPreExecute
在doInBackground
中获取EditText
值,在doInBackground
执行完成时使用onPostExecute
更新Ui
编辑:
根据OP请求,将所有验证移动到内部按钮点击为:
registerButton.setOnClickListener(new View.OnClickListener() {
public void onClick (View view){
// PUT YOUR ALL VALIDATION HERE
if(/* if validation success */){
// start AsyncTask here
new Registeruser().execute();
}
else{
// show validation error here
}
}
});
尝试在doInBackground((方法中删除Toasts
。当您访问doInBackground()
中的UI以显示祝酒词时,甚至为什么不能在启动异步任务之前移动验证部分。
我认为应该使用RunOnuithread
方法,或者在Asynctask
方法的doInBackground()
中使用Handler
While。
这种方式使用
@Override
protected Long doInBackground(String... urls) {
// Already suggested by Dixit Patel:
// Use the runOnUiThread method.
// See his explanation.
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!password.equals(confirmpassword)) {
Toast.makeText(getApplicationContext(),
"The password does not match.",
Toast.LENGTH_SHORT).show();
newPassword.setText("");
newConfiPass.setText("");
}
}
});