我在写的注册页面,本来是代码是
public class MainActivity extends AppCompatActivity {
private TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText resid=(EditText) findViewById(R.id.etresid);
final EditText firstname=(EditText) findViewById(R.id.etfirstname);
final EditText surname=(EditText) findViewById(R.id.etsurname);
final EditText dob=(EditText) findViewById(R.id.etdob);
final EditText address=(EditText) findViewById(R.id.etaddress);
final EditText postcode=(EditText) findViewById(R.id.etpostcode);
final EditText email=(EditText) findViewById(R.id.etemail);
final EditText mobile=(EditText) findViewById(R.id.etmobile);
final EditText numofresidents=(EditText) findViewById(R.id.etnumofresidents);
final EditText nameofenergy=(EditText) findViewById(R.id.etnameofenergy);
Button addResident=(Button)findViewById(R.id.btnAddUser);
addResident.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
Resident resident=new Resident(
Integer.valueOf(params[0]),params[1],params[2], Date.valueOf(params[3]),params[4],
Integer.valueOf(params[5]),params[6],Integer.valueOf(params[7]),Integer.valueOf(params[8]),params[9]);
RestClient.createResident(resident);
return "User was added";
}
protected void onPostExecute(String response) {
resultTextView.setText(response);
}
}.execute(resid.getText().toString(),firstname.getText().toString(),surname.getText().toString()
,dob.getText().toString(),address.getText().toString(),postcode.getText().toString(),email.getText().toString()
,mobile.getText().toString(),numofresidents.getText().toString(),nameofenergy.getText().toString());
}
});
}
}
这个代码可以运行并且进行注册操作,但是我们要求将最后两个改用spinner,因此我将代码改成了
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register_page);
final EditText resid=(EditText) findViewById(R.id.etresid);
final EditText firstname=(EditText) findViewById(R.id.etfirstname);
final EditText surname=(EditText) findViewById(R.id.etsurname);
final EditText dob=(EditText) findViewById(R.id.etdob);
final EditText address=(EditText) findViewById(R.id.etaddress);
final EditText postcode=(EditText) findViewById(R.id.etpostcode);
final EditText email=(EditText) findViewById(R.id.etemail);
final EditText mobile=(EditText) findViewById(R.id.etmobile);
final Spinner resident=(Spinner) findViewById(R.id.etnumofresidents);
final Spinner energy=(Spinner) findViewById(R.id.etnameofenergy);
reslist = new ArrayList<Integer>();
reslist.add(1);
reslist.add(2);
reslist.add(3);
reslist.add(4);
reslist.add(5);
resadapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_dropdown_item,reslist);
resadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
resident.setAdapter(resadapter);
enerlist = new ArrayList<String>();
enerlist.add("ABB");
enerlist.add("ABC");
enerlist.add("DDD");
eneadapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,enerlist);
eneadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
energy.setAdapter(eneadapter);
Button addResident=(Button)findViewById(R.id.btnAddUser);
addResident.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
Resident resident=new Resident(
Integer.valueOf(params[0]),params[1],params[2], Date.valueOf(params[3]),params[4],
Integer.valueOf(params[5]),params[6],Integer.valueOf(params[7]),Integer.valueOf(params[8]),params[9]);
RestClient.createResident(resident);
return "User was added";
}
protected void onPostExecute(String response) {
resultTextView.setText(response);
}
}.execute(resid.getText().toString(),firstname.getText().toString(),surname.getText().toString()
,dob.getText().toString(),address.getText().toString(),postcode.getText().toString(),email.getText().toString()
,mobile.getText().toString(),resident.toString(),energy.toString());
}
});
}
}
这个时候运行时,进行注册操作就开始报错了,项目会强行退出,错误提示
04-26 08:34:00.884 17492-17515/ass2.assignment2 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: ass2.assignment2, PID: 17492
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.IllegalArgumentException
at java.sql.Date.valueOf(Date.java:142)
** at ass2.assignment2.main.RegisterPage$1$1.doInBackground(RegisterPage.java:75)
at ass2.assignment2.main.RegisterPage$1$1.doInBackground(RegisterPage.java:71)**
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)