当我将数据插入数据库时,我很难修复错误,这是我的代码:
package com.example.pr;
import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class addcustomer extends Activity {
private int customer_id;
private string name;
private int passportNo;
private int ID;
private string addr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_cusact);
EditText txtName =(EditText) findViewById(R.id.editText1);
EditText CusID =(EditText) findViewById(R.id.editText2);
EditText Pass =(EditText) findViewById(R.id.editText3);
EditText Addr =(EditText) findViewById(R.id.editText5);
final int CusID1;
final int pass1;
Button btnAddNewcustomer = (Button) findViewById(R.id.button1);
final String txtname1=txtName.getText().toString();
if(CusID.getText().toString()!="")
CusID1=Integer.parseInt(CusID.getText().toString());
else
CusID1=0;
if(Pass.getText().toString()!="")
pass1=Integer.parseInt(Pass.getText().toString());
else
pass1=0;
final String Addr1=Addr.getText().toString();
btnAddNewcustomer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
insertData(txtname1,CusID1,pass1,Addr1);
Toast.makeText(addcustomer.this,"added", Toast.LENGTH_LONG).show();
finish();
}
});
}
private void insertData(String name,int CUSID,int PASSN, String ADDRN) {
// TODO Auto-generated method stub
AndroidDbHelper helper = new AndroidDbHelper(this);
SQLiteDatabase database = helper.getWritableDatabase();
ContentValues content = new ContentValues();
content.put(AndroidDbHelper.COLUMN_NAME, name);
content.put(AndroidDbHelper.COLUMN_NAME2, CUSID);
content.put(AndroidDbHelper.COLUMN_NAME3, PASSN);
content.put(AndroidDbHelper.COLUMN_NAME4, ADDRN);
database.insert(AndroidDbHelper.TABLE_NAME, null, content);
}
}
private void insertData(String name,int CUSID,int PASSN, String ADDRN) {
// TODO Auto-generated method stub
AndroidDbHelper helper = new AndroidDbHelper(this);
SQLiteDatabase database = helper.getWritableDatabase();
ContentValues content = new ContentValues();
content.put(AndroidDbHelper.COLUMN_NAME, name);
content.put(AndroidDbHelper.COLUMN_NAME2, CUSID);
content.put(AndroidDbHelper.COLUMN_NAME3, PASSN);
content.put(AndroidDbHelper.COLUMN_NAME4, ADDRN);
database.insert(AndroidDbHelper.TABLE_NAME, null, content);
}
}
这里是用于创建数据库的数据库助手类 看起来工作正常
package com.example.pr;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class AndroidDbHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "StudentData";
public static final int DB_VERSION = 1;
public static final String TABLE_NAME = "student";
public static final String COLUMN_NAME= "NAME";
public static final String COLUMN_NAME2= "CUSTOMERID";
public static final String COLUMN_NAME3= "PASSPORT";
public static final String COLUMN_NAME4= "ADDR";
public AndroidDbHelper(Context context) {
super(context, DB_NAME, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
String CreateTable = "CREATE TABLE" + TABLE_NAME + "(" +"ID INTEGER primary key AUTOINCREMENT,"
+ "NAME VARCAR NOT NULL,"
+ "CUSTOMERID INTEGER,"
+ "PASSPORT INT,"
+ "ADDR VARCHAR NOT NULL"+");";
db.execSQL(CreateTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME );
onCreate(db);
}
}
应用程序崩溃了!
这是我的日志:
04-11 14:15:36.860: I/Choreographer(1171): Skipped 53 frames! The application may be doing too much work on its main thread.
04-11 14:15:37.200: I/Choreographer(1171): Skipped 395 frames! The application may be doing too much work on its main thread.
04-11 14:15:37.330: D/gralloc_goldfish(1171): Emulator without GPU emulation detected.
04-11 14:15:37.870: I/Choreographer(1171): Skipped 483 frames! The application may be doing too much work on its main thread.
04-11 14:18:30.030: I/Choreographer(1171): Skipped 225 frames! The application may be doing too much work on its main thread.
04-11 14:18:31.450: I/Choreographer(1171): Skipped 47 frames! The application may be doing too much work on its main thread.
04-11 14:18:32.410: I/Choreographer(1171): Skipped 281 frames! The application may be doing too much work on its main thread.
04-11 14:18:34.670: I/Choreographer(1171): Skipped 150 frames! The application may be doing too much work on its main thread.
04-11 14:18:36.050: I/Choreographer(1171): Skipped 256 frames! The application may be doing too much work on its main thread.
04-11 14:18:39.330: D/AndroidRuntime(1171): Shutting down VM
04-11 14:18:39.330: W/dalvikvm(1171): threadid=1: thread exiting with uncaught exception (group=0xb4adbb90)
04-11 14:18:39.560: E/AndroidRuntime(1171): FATAL EXCEPTION: main
04-11 14:18:39.560: E/AndroidRuntime(1171): Process: com.example.pr, PID: 1171
04-11 14:18:39.560: E/AndroidRuntime(1171): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pr/com.example.pr.addcustomer}: java.lang.NumberFormatException: Invalid int: ""
04-11 14:18:39.560: E/AndroidRuntime(1171): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
04-11 14:18:39.560: E/AndroidRuntime(1171): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
04-11 14:18:39.560: E/AndroidRuntime(1171): at android.app.ActivityThread.access$700(ActivityThread.java:135)
04-11 14:18:39.560: E/AndroidRuntime(1171): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-11 14:18:39.560: E/AndroidRuntime(1171): at android.os.Handler.dispatchMessage(Handler.java:102)
04-11 14:18:39.560: E/AndroidRuntime(1171): at android.os.Looper.loop(Looper.java:137)
04-11 14:18:39.560: E/AndroidRuntime(1171): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-11 14:18:39.560: E/AndroidRuntime(1171): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 14:18:39.560: E/AndroidRuntime(1171): at java.lang.reflect.Method.invoke(Method.java:515)
04-11 14:18:39.560: E/AndroidRuntime(1171): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-11 14:18:39.560: E/AndroidRuntime(1171): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-11 14:18:39.560: E/AndroidRuntime(1171): at dalvik.system.NativeStart.main(Native Method)
04-11 14:18:39.560: E/AndroidRuntime(1171): Caused by: java.lang.NumberFormatException: Invalid int: ""
04-11 14:18:39.560: E/AndroidRuntime(1171): at java.lang.Integer.invalidInt(Integer.java:137)
04-11 14:18:39.560: E/AndroidRuntime(1171): at java.lang.Integer.parseInt(Integer.java:358)
04-11 14:18:39.560: E/AndroidRuntime(1171): at java.lang.Integer.parseInt(Integer.java:331)
04-11 14:18:39.560: E/AndroidRuntime(1171): at com.example.pr.addcustomer.onCreate(addcustomer.java:63)
04-11 14:18:39.560: E/AndroidRuntime(1171): at android.app.Activity.performCreate(Activity.java:5243)
04-11 14:18:39.560: E/AndroidRuntime(1171): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-11 14:18:39.560: E/AndroidRuntime(1171): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
04-11 14:18:39.560: E/AndroidRuntime(1171): ... 11 more
04-11 14:18:40.160: D/dalvikvm(1171): GC_FOR_ALLOC freed 105K, 6% free 3078K/3252K, paused 527ms, total 540ms
04-11 14:18:45.690: I/Process(1171): Sending signal. PID: 1171 SIG: 9
04-11 14:24:50.490: I/Choreographer(1226): Skipped 35 frames! The application may be doing too much work on its main thread.
04-11 14:24:50.780: I/Choreographer(1226): Skipped 354 frames! The application may be doing too much work on its main thread.
04-11 14:24:50.810: D/gralloc_goldfish(1226): Emulator without GPU emulation detected.
04-11 14:24:51.030: I/Choreographer(1226): Skipped 87 frames! The application may be doing too much work on its main thread.
04-11 14:24:54.700: I/Choreographer(1226): Skipped 240 frames! The application may be doing too much work on its main thread.
04-11 14:24:56.140: I/Choreographer(1226): Skipped 179 frames! The application may be doing too much work on its main thread.
04-11 14:24:56.970: I/Choreographer(1226): Skipped 195 frames! The application may be doing too much work on its main thread.
04-11 14:24:58.700: I/Choreographer(1226): Skipped 134 frames! The application may be doing too much work on its main thread.
04-11 14:25:00.470: I/Choreographer(1226): Skipped 336 frames! The application may be doing too much work on its main thread.
04-11 14:25:04.310: D/AndroidRuntime(1226): Shutting down VM
04-11 14:25:04.310: W/dalvikvm(1226): threadid=1: thread exiting with uncaught exception (group=0xb4adbb90)
04-11 14:25:04.440: E/AndroidRuntime(1226): FATAL EXCEPTION: main
04-11 14:25:04.440: E/AndroidRuntime(1226): Process: com.example.pr, PID: 1226
04-11 14:25:04.440: E/AndroidRuntime(1226): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pr/com.example.pr.addcustomer}: java.lang.NumberFormatException: Invalid int: ""
04-11 14:25:04.440: E/AndroidRuntime(1226): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
04-11 14:25:04.440: E/AndroidRuntime(1226): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
04-11 14:25:04.440: E/AndroidRuntime(1226): at android.app.ActivityThread.access$700(ActivityThread.java:135)
04-11 14:25:04.440: E/AndroidRuntime(1226): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-11 14:25:04.440: E/AndroidRuntime(1226): at android.os.Handler.dispatchMessage(Handler.java:102)
04-11 14:25:04.440: E/AndroidRuntime(1226): at android.os.Looper.loop(Looper.java:137)
04-11 14:25:04.440: E/AndroidRuntime(1226): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-11 14:25:04.440: E/AndroidRuntime(1226): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 14:25:04.440: E/AndroidRuntime(1226): at java.lang.reflect.Method.invoke(Method.java:515)
04-11 14:25:04.440: E/AndroidRuntime(1226): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-11 14:25:04.440: E/AndroidRuntime(1226): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-11 14:25:04.440: E/AndroidRuntime(1226): at dalvik.system.NativeStart.main(Native Method)
04-11 14:25:04.440: E/AndroidRuntime(1226): Caused by: java.lang.NumberFormatException: Invalid int: ""
04-11 14:25:04.440: E/AndroidRuntime(1226): at java.lang.Integer.invalidInt(Integer.java:137)
04-11 14:25:04.440: E/AndroidRuntime(1226): at java.lang.Integer.parseInt(Integer.java:358)
04-11 14:25:04.440: E/AndroidRuntime(1226): at java.lang.Integer.parseInt(Integer.java:331)
04-11 14:25:04.440: E/AndroidRuntime(1226): at com.example.pr.addcustomer.onCreate(addcustomer.java:63)
04-11 14:25:04.440: E/AndroidRuntime(1226): at android.app.Activity.performCreate(Activity.java:5243)
04-11 14:25:04.440: E/AndroidRuntime(1226): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-11 14:25:04.440: E/AndroidRuntime(1226): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
04-11 14:25:04.440: E/AndroidRuntime(1226): ... 11 more
04-11 14:25:05.040: D/dalvikvm(1226): GC_FOR_ALLOC freed 107K, 6% free 3077K/3252K, paused 576ms, total 588ms
答案 0 :(得分:0)
你写了"CREATE TABLE" + TABLE_NAME
- 它会是CREATE TABLEStudentData
。您应该在TABLE
字后添加空格。
答案 1 :(得分:0)
int CusID1=Integer.parseInt(CusID.getText().toString());
/////
int pass1=Integer.parseInt(Pass.getText().toString());`
但到目前为止,CusID
和Pass
都没有数据,这就是为什么你会得到NumberFormatException