2017-04-23 75 views
1

想知道您是否可以幫助我。添加第二張表後Android Studio應用崩潰

我有一個應用程序與1表存儲用戶的詳細信息,一個工作正常,允許新用戶註冊和登錄。但後來我添加了一個新表來存儲事務,即使我以完全相同的方式添加了第二個表,我仍然無法獲得第二個表的工作,但我已嘗試升級數據庫版本,但是我將其重新轉換爲之前在git上的提交不起作用。

繼承人什麼我在我的DBhelper兩個表

public class DBHandler extends SQLiteOpenHelper { 

    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_NAME = "user.db"; 
    public static final String TABLE_USER = "user"; 
    public static final String COLUMN_ID = "_id"; 
    public static final String COLUMN_NAME = "name"; 
    public static final String COLUMN_ADDRESS1 = "address1"; 
    public static final String COLUMN_ADDRESS2 = "address2"; 
    public static final String COLUMN_ACCNO = "accNo"; 
    public static final String COLUMN_PIN = "PIN"; 
    public static final String COLUMN_BALANCE = "currentbalance"; 

    public static final String TABLE_TRANSACTIONS = "transactions"; 
    public static final String COLUMN_TID = "_tid"; 
    public static final String COLUMN_DESCRIPTION = "description"; 
    public static final String COLUMN_AMOUNT = "amount"; 


    public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 
     super(context, DATABASE_NAME, factory, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

     String query = "CREATE TABLE " + TABLE_USER + "(" + 
       COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       COLUMN_NAME + " TEXT, " + 
       COLUMN_ADDRESS1 + " TEXT, " + 
       COLUMN_ADDRESS2 + " TEXT, " + 
       COLUMN_ACCNO + " INTEGER, " + 
       COLUMN_PIN + " INTEGER UNIQUE, " + 
       COLUMN_BALANCE + " INTEGER " + 
       ");"; 

     db.execSQL(query); 

     String query2 = "CREATE TABLE " + TABLE_TRANSACTIONS + "(" + 
       COLUMN_TID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       COLUMN_DESCRIPTION + " TEXT, " + 
       COLUMN_AMOUNT + " INTEGER);"; 

     db.execSQL(query2); 


    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_TRANSACTIONS); 
     onCreate(db); 
    } 

    //Add new row 
    public void addUser(User user){ 
     ContentValues values = new ContentValues(); 
     values.put(COLUMN_NAME, user.get_name()); 
     values.put(COLUMN_ADDRESS1, user.get_address1()); 
     values.put(COLUMN_ADDRESS2, user.get_address2()); 
     values.put(COLUMN_ACCNO, user.get_accNo()); 
     values.put(COLUMN_PIN, user.get_PIN()); 
     values.put(COLUMN_BALANCE, user.get_currentbalance()); 

     SQLiteDatabase db = getWritableDatabase(); 
     db.insert(TABLE_USER, null, values); 
     db.close(); 
    } 
    public void addTransaction(Transaction transaction){ 
     ContentValues values = new ContentValues(); 
     values.put(COLUMN_DESCRIPTION, transaction.get_description()); 
     values.put(COLUMN_AMOUNT, transaction.get_amount()); 

     SQLiteDatabase db = getWritableDatabase(); 
     db.insert(TABLE_TRANSACTIONS, null, values); 
     db.close(); 
    } 

它需要一個「交易」對象,我對這裏的構造,我沒有包括在這裏的getter/setter代碼因爲它只是簡單的

public class Transaction { 

    private int _id; 
    private String _description; 
    private double _amount; 

    public Transaction(String _description, double _amount) { 
     this._description = _description; 
     this._amount = _amount; 
    } 

這裏是堆棧跟蹤:

04-23 23:33:21.425 18370-18370/marcusobyrne.bankingapp E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: marcusobyrne.bankingapp, PID: 18370 
                     java.lang.IllegalStateException: Could not find method saveTransaction(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button2' 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
                      at android.view.View.performClick(View.java:6207) 
                      at android.widget.TextView.performClick(TextView.java:11094) 
                      at android.view.View$PerformClick.run(View.java:23639) 
                      at android.os.Handler.handleCallback(Handler.java:751) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6688) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
從INTEGER到REAL
+1

提供確切的崩潰消息/堆棧跟蹤以及任何已經嘗試過的修復程序。 – alzee

+0

@alzee不幸的是,我的電腦無法運行虛擬仿真器,我通過手機上的開發人員模式運行它,問題的另一部分:( – azamean

+0

即使您正在通過手機運行,也可以獲取堆棧跟蹤,而不需要 –

回答

0

變化DATABASE_VERSION至2

private static final int DATABASE_VERSION = 2; 

變化COLUMN_AMOUNT。

+0

我試過了,它沒有幫助,我把它重新放回到1,因爲它也開始導致現有的用戶表崩潰! – azamean

+0

@azamean在你的問題中添加錯誤日誌 –

相關問題