qq_38034939
qq_38034939
2017-12-23 11:47

android 和sqlite 问题? 小白不会 大佬们看一看!

5
  • exception
  • sqlite
  • android

om.example.lenovo.ciba E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lenovo.ciba, PID: 19916
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6213)
at android.widget.TextView.performClick(TextView.java:11074)
at android.view.View$PerformClick.run(View.java:23645)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6646)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6213) 
at android.widget.TextView.performClick(TextView.java:11074) 
at android.view.View$PerformClick.run(View.java:23645) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6646) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:533)
at java.lang.Integer.parseInt(Integer.java:556)
at com.example.lenovo.ciba.BasestaffActivity.addStaff(BasestaffActivity.java:30)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:6213) 
at android.widget.TextView.performClick(TextView.java:11074) 
at android.view.View$PerformClick.run(View.java:23645) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6646) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

package com.example.lenovo.ciba;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class BasestaffActivity extends AppCompatActivity {
private EditText etSname, etAge,etXinzi,etTel,etZhiwei,etBumen;
private TextView tvShow;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basestaff);
this.etSname = (EditText) findViewById(R.id.edit_staff);
this.etAge = (EditText) findViewById(R.id.edit_age);
this.etXinzi = (EditText) findViewById(R.id.edit_xinzi);
this.etTel = (EditText) findViewById(R.id.edit_Tel);
this.etZhiwei = (EditText) findViewById(R.id.edit_zhiwei);
this.etBumen = (EditText) findViewById(R.id.edit_bumen);
this.tvShow = (TextView) findViewById(R.id.textStaff);}

// 添加员工
public void addStaff(View view){
    // 获取输入的员工数据
    String sname = etSname.getText().toString();
    int age = Integer.parseInt(etAge.getText().toString());
    int xinzi  = Integer.parseInt(etXinzi.getText().toString());
    int tel = Integer.parseInt(etTel.getText().toString());
    String zhiwei = etZhiwei.getText().toString();
    String bumen = etBumen.getText().toString();
    // 向数据库表中添加员工
    // 1. 实例化数据库的操作类
    SQLiteDBHelper sqLiteDBHelper = new SQLiteDBHelper(getApplicationContext());
    // 2. 通过操作类获取数据库实例
    SQLiteDatabase sqLiteDatabase = sqLiteDBHelper.getWritableDatabase();
    // 3. 通过数据库实例执行SQL命令
    try {
        sqLiteDatabase.execSQL("insert into student(sname,age,tel,xinzi,zhiwei,bumen) values(?,?,?,?,?,?)", new Object[]{sname, age,tel,xinzi,zhiwei,bumen});
        Toast.makeText(this, "员工信息添加成功!", Toast.LENGTH_SHORT).show();
        etSname.setText("");
        etAge.setText("");
        etXinzi.setText("");
        etTel.setText("");
        etZhiwei.setText("");
        etTel.setText("");
    }catch (Exception e){
        e.printStackTrace();
        Toast.makeText(this, "员工信息添加失败!", Toast.LENGTH_SHORT).show();
    }
}

}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

8条回答

为你推荐

换一换