添加第二个表后Android Studio应用程序崩溃

时间:2017-04-23 21:26:03

标签: java android sqlite

想知道你是否可以帮助我。

我有一个带有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)

1 个答案:

答案 0 :(得分:0)

将DATABASE_VERSION更改为2

 private static final int DATABASE_VERSION = 2;

将COLUMN_AMOUNT从INTEGER更改为REAL。