gpt4 book ai didi

java - Android - 更改表格列现在会使程序崩溃。 java.lang.NumberFormatException : Invalid int: "T" 异常

转载 作者:行者123 更新时间:2023-11-30 01:34:10 25 4
gpt4 key购买 nike

我正在更改我的数据库结构,因为我注意到有些事情的顺序不正确。这样做之后,它会弹出一条错误消息

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);
}
}

DB.java

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;
}

}

tableStudents.java

public class tableStudents {
public String name, gender, password, course, modules;
public int age;
//Constructor
public tableStudents()
{

}

//constructor
public tableStudents(String name, String gender, int age, String password, String course, String modules)
{
this.name = name;
this.password = password;
this.gender = gender;
this.age = age;
this.course = course;
this.modules = modules;
}


public static abstract class tableColumns implements BaseColumns
{
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 static final String Database = "databasename";
public static final String Table = "tablename";
}


}

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() 的开头试试这个:

deleteDatabase("your_db_name");

关于java - Android - 更改表格列现在会使程序崩溃。 java.lang.NumberFormatException : Invalid int: "T" 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35324586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com