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

安卓开发中,注册页面连接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])哇 你看看这个参数格式对不对咯

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

报告相同问题?

悬赏问题

  • ¥30 软件自定义无线电该怎样使用
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下