これを実行すると、力が入ります。事はです。データは私のデータベースに送信されます。それは私のデータベースにありますが、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