04-06 13:29:26.403 16654-17357/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.donateblood.blooddonation, PID: 16654
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: android.util.AndroidRuntimeException: Animators may only be run on Looper threads
at android.animation.ValueAnimator.start(ValueAnimator.java:946)
at android.animation.ValueAnimator.start(ValueAnimator.java:969)
at android.animation.ObjectAnimator.start(ObjectAnimator.java:829)
at android.animation.AnimatorSet.start(AnimatorSet.java:572)
at android.animation.StateListAnimator.start(StateListAnimator.java:156)
at android.animation.StateListAnimator.setState(StateListAnimator.java:149)
at android.view.View.drawableStateChanged(View.java:15994)
at android.widget.TextView.drawableStateChanged(TextView.java:3675)
at android.support.v7.widget.AppCompatButton.drawableStateChanged(AppCompatButton.java:143)
at android.view.View.refreshDrawableState(View.java:16023)
at android.view.View.setEnabled(View.java:6723)
at android.widget.TextView.setEnabled(TextView.java:1457)
at com.donateblood.blooddonation.SignupActivity.onSignupSuccess(SignupActivity.java:81)
at com.donateblood.blooddonation.SignupActivity.addUsertoDB(SignupActivity.java:183)
at com.donateblood.blooddonation.SignupActivity$dbAsync.doInBackground(SignupActivity.java:152)
at com.donateblood.blooddonation.SignupActivity$dbAsync.doInBackground(SignupActivity.java:137)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
package com.donateblood.blooddonation;
/**
* Created by YouCaf Iqbal on 4/5/2016.
*/
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import butterknife.ButterKnife;
import butterknife.InjectView;
public class SignupActivity extends AppCompatActivity {
private static final String TAG = "SignupActivity";
DBCollection collection;
DB db;
@InjectView(R.id.input_name) EditText _nameText;
@InjectView(R.id.input_email) EditText _emailText;
@InjectView(R.id.input_password) EditText _passwordText;
@InjectView(R.id.btn_signup) Button _signupButton;
@InjectView(R.id.link_login) TextView _loginLink;
@InjectView(R.id.input_location) TextView _locText;
@InjectView(R.id.input_number) TextView _numText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
Spinner spinner =(Spinner) findViewById(R.id.spinner);
String[] list = getResources().getStringArray(R.array.blood_type);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.spinner_layout,R.id.txt,list);
spinner.setAdapter(adapter);
ButterKnife.inject(this);
_signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signup();
}
});
_loginLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Finish the registration screen and return to the Login activity
finish();
}
});
}
public void signup() {
Log.d(TAG, "Signup");
if (validate()==false) {
onSignupFailed();
return;
}
_signupButton.setEnabled(false);
dbAsync addtobd = new dbAsync();
addtobd.execute();
}
public void onSignupSuccess() {
_signupButton.setEnabled(true);
setResult(RESULT_OK, null);
finish();
}
public void onSignupFailed() {
Toast.makeText(getBaseContext(), "Sign Up failed", Toast.LENGTH_LONG).show();
_signupButton.setEnabled(true);
}
public boolean validate() {
boolean valid = true;
String name = _nameText.getText().toString();
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
String location = _locText.getText().toString();
String number = _numText.getText().toString();
if (name.isEmpty() || name.length() < 3) {
_nameText.setError("at least 3 characters");
valid = false;
} else {
_nameText.setError(null);
}
if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
_emailText.setError("enter a valid email address");
valid = false;
} else {
_emailText.setError(null);
}
if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
_passwordText.setError("between 4 and 10 alphanumeric characters");
valid = false;
} else {
_passwordText.setError(null);
}
if (location.isEmpty()) {
_locText.setError("Enter location");
valid = false;
} else {
_locText.setError(null);
}
if (number.isEmpty()) {
_numText.setError("Enter valid number");
valid = false;
} else {
_numText.setError(null);
}
return valid;
}
public class dbAsync extends AsyncTask<Void,Void,Void> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SignupActivity.this);
pDialog.setMessage("Creating Account");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Void doInBackground(Void... voids) {
addUsertoDB();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
pDialog.dismiss();
Toast.makeText(getBaseContext(), "Created Successfully", Toast.LENGTH_SHORT).show();
}
}
public void addUsertoDB(){
MongoClientURI uri = new MongoClientURI("mongodb://yousaf:UC1941994@ds015730.mlab.com:15730/donateblood");
MongoClient client = null;
client = new MongoClient(uri);
db = client.getDB(uri.getDatabase());
String name = _nameText.getText().toString();
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
String location = _locText.getText().toString();
String number = _numText.getText().toString();
collection = db.getCollection("UserDetails");
BasicDBObject document = new BasicDBObject();
document.put("Name", name);
document.put("email", email);
document.put("password", password);
document.put("location", location);
document.put("number", number);
collection.insert(document);
onSignupSuccess();
}
}
当我点击创建帐户时,进度条显示创建帐户,然后在某个时间“不幸的应用程序停止工作”消息到来并停止但值已成功添加到数据库。
答案 0 :(得分:2)
尝试将onSignupSuccess()
放入onPostExecute()
修改强>
您正在尝试在不是UIThread的线程中修改您的UI,这会导致您的错误,doInBackground()
调用addUsertoDB()
调用
修改用户界面的onSignupSuccess();
,在UIThread
onSignupSuccess();