Android - 更改表列现在会导致程序崩溃。 java.lang.NumberFormatException:无效的int:“T”
Posted
技术标签:
【中文标题】Android - 更改表列现在会导致程序崩溃。 java.lang.NumberFormatException:无效的int:“T”【英文标题】:Android - Altering table columns now crashes program. java.lang.NumberFormatException: Invalid int: "T" 【发布时间】:2016-05-21 08:36:36 【问题描述】:我正在更改我的数据库结构,因为我注意到有些事情的顺序不正确。这样做之后,它会闪现一个错误,说
02-10 19:42:06.562: E/androidRuntime(1215): java.lang.RuntimeException: Unable to start activity ComponentInfocom.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 ComponentInfocom.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)
【问题讨论】:
【参考方案1】:在 onCreate() 的开头试试这个:
deleteDatabase("your_db_name");
【讨论】:
恐怕这不起作用,- 方法 deleteDatabase(SQLiteDatabase) 未定义类型 DB【参考方案2】:您是否尝试过删除应用程序的数据然后再次运行它?
【讨论】:
我该怎么做呢?我现在要发布我的整个日志猫 并不复杂,只需打开设置,进入应用,选择您的应用并清除其数据 我的意思是在 Eclipse 本身上,我目前无法使用 Android 手机 您也可以在活动中运行 deleteDatabase("database_name") 或直接卸载您的应用 我尝试将它放入 db.java 的 onCreate 中,但没有成功以上是关于Android - 更改表列现在会导致程序崩溃。 java.lang.NumberFormatException:无效的int:“T”的主要内容,如果未能解决你的问题,请参考以下文章
如果 Android 服务崩溃,它会导致它正在运行的应用程序崩溃吗?
Android : Kotlin : MVVM : 为啥 viewModel.onButtonClicked() 会导致应用程序崩溃?