当我执行此操作时,我正在关闭一个力量。事情是。数据确实会发送到我的数据库。所以它在我的数据库中,但是我读了我的logcat,它说我的DinBackground中有问题,这是我的代码:
// 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_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
// Importing all assets like buttons, text fields
inputFullName = (EditText) findViewById(R.id.registerName);
inputEmail = (EditText) findViewById(R.id.registerEmail);
inputPassword = (EditText) findViewById(R.id.registerPassword);
btnRegister = (Button) findViewById(R.id.btnRegister);
btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
registerErrorMsg = (TextView) findViewById(R.id.register_error);
// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
new CreateNewProduct().execute();
}
});
}
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(RegisterActivity.this);
pDialog.setMessage("Loading");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String name = inputFullName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.registerUser(name, email, password);
// 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_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
// Close Registration Screen
finish();
}else{
// Error in registration
registerErrorMsg.setText("Error occured in registration");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
}
}
这是我的logcat:
12-02 15:35:33.761: E/AndroidRuntime(671): FATAL EXCEPTION: AsyncTask #1
12-02 15:35:33.761: E/AndroidRuntime(671): java.lang.RuntimeException: An error occured while executing doInBackground()
12-02 15:35:33.761: E/AndroidRuntime(671): at android.os.AsyncTask$3.done(AsyncTask.java:278)
12-02 15:35:33.761: E/AndroidRuntime(671): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-02 15:35:33.761: E/AndroidRuntime(671): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-02 15:35:33.761: E/AndroidRuntime(671): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-02 15:35:33.761: E/AndroidRuntime(671): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-02 15:35:33.761: E/AndroidRuntime(671): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
12-02 15:35:33.761: E/AndroidRuntime(671): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-02 15:35:33.761: E/AndroidRuntime(671): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-02 15:35:33.761: E/AndroidRuntime(671): at java.lang.Thread.run(Thread.java:856)
12-02 15:35:33.761: E/AndroidRuntime(671): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-02 15:35:33.761: E/AndroidRuntime(671): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
12-02 15:35:33.761: E/AndroidRuntime(671): at android.view.ViewRootImpl.invalidateChild(ViewRootImpl.java:722)
12-02 15:35:33.761: E/AndroidRuntime(671): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:771)
12-02 15:35:33.761: E/AndroidRuntime(671): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4005)
12-02 15:35:33.761: E/AndroidRuntime(671): at android.view.View.invalidate(View.java:8576)
12-02 15:35:33.761: E/AndroidRuntime(671): at android.view.View.invalidate(View.java:8527)
12-02 15:35:33.761: E/AndroidRuntime(671): at android.widget.TextView.checkForRelayout(TextView.java:6760)
12-02 15:35:33.761: E/AndroidRuntime(671): at android.widget.TextView.setText(TextView.java:3306)
12-02 15:35:33.761: E/AndroidRuntime(671): at android.widget.TextView.setText(TextView.java:3162)
12-02 15:35:33.761: E/AndroidRuntime(671): at android.widget.TextView.setText(TextView.java:3137)
12-02 15:35:33.761: E/AndroidRuntime(671): at com.laurenswuyts.find.it.RegisterActivity$CreateNewProduct.doInBackground(RegisterActivity.java:100)
12-02 15:35:33.761: E/AndroidRuntime(671): at com.laurenswuyts.find.it.RegisterActivity$CreateNewProduct.doInBackground(RegisterActivity.java:1)
12-02 15:35:33.761: E/AndroidRuntime(671): at android.os.AsyncTask$2.call(AsyncTask.java:264)
12-02 15:35:33.761: E/AndroidRuntime(671): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-02 15:35:33.761: E/AndroidRuntime(671): ... 5 more
答案 0 :(得分:0)
您正在 doInBackground()中访问 UI元素。顾名思义,doInBackgroud()是后台(非UI)线程,并且无权访问UI元素。因此,您应该将该代码放在 onPreExecute()
(这是一个UI线程)中。从onPreExecute中的UI元素获取用户输入并将它们存储在Strings中,稍后在doInBackground()中使用它们。
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
String name, email, password;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(RegisterActivity.this);
pDialog.setMessage("Loading");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
name = inputFullName.getText().toString();
email = inputEmail.getText().toString();
password = inputPassword.getText().toString();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.registerUser(name, email, password);
// 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_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
// Close Registration Screen
finish();
}else{
// Error in registration
//show error message to user in onPostExecute
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
}
}
此外, 调用AsyncTask时参数错误。
由于您已将其声明为class CreateNewProduct extends AsyncTask<String, String, String>
,因此在调用AsyncTask时需要传递一个String。
因此,new CreateNewProduct().execute("");
是调用它的正确方法。