2016-02-10 88 views
1

我正在改變我的數據庫結構,因爲我注意到有些事情沒有按照正確的順序。這樣做之後,它閃爍了一個錯誤,說Android - 改變表列現在崩潰程序。 java.lang.NumberFormatException:無效int:「T」

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) 

回答

0

你試圖刪除的應用程序的數據,然後再次運行呢?

+0

我如何去這樣做呢?我現在將發佈我的整個日誌貓 –

+0

不復雜,只需打開設置,轉到應用程序,選擇您的應用程序並清除其數據 –

+0

我的意思是在Eclipse本身,我沒有在當時無法訪問Android手機 –

1

嘗試這的onCreate()的開頭:

deleteDatabase("your_db_name"); 
+0

這不會令人擔心, - 類型DB的方法deleteDatabase(SQLiteDatabase)未定義 –

相關問題