2017-04-23 99 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在你的问题中添加错误日志 –