fatesses 2018-04-26 09:18 采纳率: 50%
浏览 1563
已采纳

安卓开发中,注册页面连接restful,FATAL EXCEPTION: AsyncTask #1

我在写的注册页面,本来是代码是
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) 

  • 写回答

1条回答 默认 最新

  • pp692319755 2018-04-26 10:01
    关注

    看报错信息 错的应该是Date.valueOf(params[3])哇 你看看这个参数格式对不对咯

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。