Android改变表列现在使程序崩溃。java.lang.NumberFormatException:无效的int:";";
我正在改变我的数据库结构,因为我注意到有些东西顺序不对。这样做之后,它会闪现一个错误,说Android改变表列现在使程序崩溃。java.lang.NumberFormatException:无效的int:";";,java,android,eclipse,sqlite,Java,Android,Eclipse,Sqlite,我正在改变我的数据库结构,因为我注意到有些东西顺序不对。这样做之后,它会闪现一个错误,说 02-10 19:42:06.562: E/AndroidRuntime(1215): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project/com.example.project.MainActivity}: java.lang.NumberFormatException: Invali
02-10 19:42:06.562: E/AndroidRuntime(1215): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project/com.example.project.MainActivity}: java.lang.NumberFormatException: Invalid int: "T"
“T”是我对以前的测试数据使用的,所以我假设数据库中已经存在以前的数据,现在随着列的变化而标记错误
我认为如果我在onCreate()的一开始就删除了数据库,那么它就会清除数据库,插入新的测试数据,并消除错误。然而,我仍然得到完全相同的错误
完整代码如下:
MainActivity.java
public class MainActivity extends Activity {
Intent appIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DB db = new DB(this);
db.insertStudent("Matt", "male", 22, "password", "computing", "modules");
List<tableStudents> outputList = db.getData();
for (tableStudents student: outputList) {
// Log your results.
Log.d("result_list", student.toString());
}
}
public void goHomepage(View v)
{
Intent intent = new Intent(MainActivity.this, Homepage.class);
startActivity(intent);
}
public void goAccount(View v)
{
Intent intent = new Intent(MainActivity.this, MyAccount.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class DB extends SQLiteOpenHelper {
public static final int db_version = 1;
public static final String Table = "Students";
public static final String Student_ID = "Student_ID";
public static final String Student_Name = "Student_Name";
public static final String Student_Password = "Student_Password";
public static final String Student_Gender = "gender";
public static final String Student_Age = "age";
public static final String Student_Course = "course";
public static final String Modules = "modules";
public DB(Context context) {
super(context, tableColumns.Database, null, db_version);
}
@Override
public void onCreate(SQLiteDatabase db) {
delete(db);
//Create Table
db.execSQL("CREATE TABLE " + Table + "(" +
Student_ID + " INTEGER PRIMARY KEY, " +
Student_Name + " TEXT, " +
Student_Password + " TEXT, " +
Student_Gender + " TEXT, " +
Student_Age + " INTEGER, " +
Student_Course + " TEXT, " +
Modules + " TEXT)");
Log.d("DB", "DB Created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Table);
onCreate(db);
}
public void delete(SQLiteDatabase db)
{
db.delete(Table, null, null);
}
public List<tableStudents> getData() {
List<tableStudents> studentList = new ArrayList<tableStudents>();
// Select All Query
String selectQuery = "SELECT * FROM " + Table;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
tableStudents student = new tableStudents();
student.name = cursor.getString(0);
student.gender = cursor.getString(1);
student.age = Integer.parseInt(cursor.getString(2));
student.password = cursor.getString(3);
student.course = cursor.getString(4);
student.modules = cursor.getString(5);
studentList.add(student);
} while (cursor.moveToNext());
}
// return contact list
return studentList;
}
public boolean insertStudent(String name, String gender, int age, String password, String course, String modules) {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Student_Name, name);
contentValues.put(Student_Password, password);
contentValues.put(Student_Gender, gender);
contentValues.put(Student_Age, age);
contentValues.put(Student_Course, course);
contentValues.put(Modules, modules);
db.insert(Table, null, contentValues);
Log.d("DB", "Inserted Successfully");
return true;
}
}
LogCat
02-10 19:56:42.132: D/DB(1741): Inserted Successfully
02-10 19:56:42.142: D/AndroidRuntime(1741): Shutting down VM
02-10 19:56:42.147: E/AndroidRuntime(1741): FATAL EXCEPTION: main
02-10 19:56:42.147: E/AndroidRuntime(1741): Process: com.example.project, PID: 1741
02-10 19:56:42.147: E/AndroidRuntime(1741): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project/com.example.project.MainActivity}: java.lang.NumberFormatException: Invalid int: "T"
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.ActivityThread.access$800(ActivityThread.java:151)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.os.Handler.dispatchMessage(Handler.java:102)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.os.Looper.loop(Looper.java:135)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.ActivityThread.main(ActivityThread.java:5257)
02-10 19:56:42.147: E/AndroidRuntime(1741): at java.lang.reflect.Method.invoke(Native Method)
02-10 19:56:42.147: E/AndroidRuntime(1741): at java.lang.reflect.Method.invoke(Method.java:372)
02-10 19:56:42.147: E/AndroidRuntime(1741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
02-10 19:56:42.147: E/AndroidRuntime(1741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
02-10 19:56:42.147: E/AndroidRuntime(1741): Caused by: java.lang.NumberFormatException: Invalid int: "T"
02-10 19:56:42.147: E/AndroidRuntime(1741): at java.lang.Integer.invalidInt(Integer.java:138)
02-10 19:56:42.147: E/AndroidRuntime(1741): at java.lang.Integer.parse(Integer.java:410)
02-10 19:56:42.147: E/AndroidRuntime(1741): at java.lang.Integer.parseInt(Integer.java:367)
02-10 19:56:42.147: E/AndroidRuntime(1741): at java.lang.Integer.parseInt(Integer.java:334)
02-10 19:56:42.147: E/AndroidRuntime(1741): at com.example.project.DB.getData(DB.java:72)
02-10 19:56:42.147: E/AndroidRuntime(1741): at com.example.project.MainActivity.onCreate(MainActivity.java:31)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.Activity.performCreate(Activity.java:5990)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
您是否已尝试删除应用程序的数据,然后再次运行它?请在onCreate()的开头尝试此操作:
我该怎么做呢?我现在就发布我的整个日志,不复杂,只需打开设置,转到应用程序,选择你的应用程序并清除其数据。我的意思是在Eclipse上,我现在没有访问Android手机的权限。你也可以运行deleteDatabase(“数据库名称”)在活动中或卸载应用程序时,我已尝试将其放入db.java中的onCreate中,但没有成功。恐怕这不起作用,-类型db的方法deleteDatabase(SQLiteDatabase)未定义
02-10 19:56:42.132: D/DB(1741): Inserted Successfully
02-10 19:56:42.142: D/AndroidRuntime(1741): Shutting down VM
02-10 19:56:42.147: E/AndroidRuntime(1741): FATAL EXCEPTION: main
02-10 19:56:42.147: E/AndroidRuntime(1741): Process: com.example.project, PID: 1741
02-10 19:56:42.147: E/AndroidRuntime(1741): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project/com.example.project.MainActivity}: java.lang.NumberFormatException: Invalid int: "T"
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.ActivityThread.access$800(ActivityThread.java:151)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.os.Handler.dispatchMessage(Handler.java:102)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.os.Looper.loop(Looper.java:135)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.ActivityThread.main(ActivityThread.java:5257)
02-10 19:56:42.147: E/AndroidRuntime(1741): at java.lang.reflect.Method.invoke(Native Method)
02-10 19:56:42.147: E/AndroidRuntime(1741): at java.lang.reflect.Method.invoke(Method.java:372)
02-10 19:56:42.147: E/AndroidRuntime(1741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
02-10 19:56:42.147: E/AndroidRuntime(1741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
02-10 19:56:42.147: E/AndroidRuntime(1741): Caused by: java.lang.NumberFormatException: Invalid int: "T"
02-10 19:56:42.147: E/AndroidRuntime(1741): at java.lang.Integer.invalidInt(Integer.java:138)
02-10 19:56:42.147: E/AndroidRuntime(1741): at java.lang.Integer.parse(Integer.java:410)
02-10 19:56:42.147: E/AndroidRuntime(1741): at java.lang.Integer.parseInt(Integer.java:367)
02-10 19:56:42.147: E/AndroidRuntime(1741): at java.lang.Integer.parseInt(Integer.java:334)
02-10 19:56:42.147: E/AndroidRuntime(1741): at com.example.project.DB.getData(DB.java:72)
02-10 19:56:42.147: E/AndroidRuntime(1741): at com.example.project.MainActivity.onCreate(MainActivity.java:31)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.Activity.performCreate(Activity.java:5990)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
02-10 19:56:42.147: E/AndroidRuntime(1741): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
deleteDatabase("your_db_name");