SQLite DbHelper不适用于ART

时间:2014-11-22 11:16:24

标签: java android sqlite

我的Android应用程序中有一个SQLite数据库但不幸的是,新的虚拟机ART它停止工作。 我在DbHelper类中得到了一个错误,特别是当我打开数据库来写/读它时。

我需要帮助!提前谢谢!

这是logCat:

11-22 11:57:05.600: E/AndroidRuntime(1435): FATAL EXCEPTION: main
11-22 11:57:05.600: E/AndroidRuntime(1435): Process: com.itc.zappapp, PID: 1435
11-22 11:57:05.600: E/AndroidRuntime(1435): java.lang.VerifyError: Rejecting class com.itc.zappapp.database.database_zappapp$DbHelper because it failed compile-time verification (declaration of 'com.itc.zappapp.database.database_zappapp$DbHelper' appears in /data/app/com.itc.zappapp-1/base.apk)
11-22 11:57:05.600: E/AndroidRuntime(1435):at com.itc.zappapp.database.database_zappapp.open(database_zappapp.java:19530)
11-22 11:57:05.600: E/AndroidRuntime(1435):at com.itc.zappapp.Activity_Head.Activity_Splash.onCreate(Activity_Splash.java:75)
11-22 11:57:05.600: E/AndroidRuntime(1435): at android.app.Activity.performCreate(Activity.java:5933)
11-22 11:57:05.600: E/AndroidRuntime(1435): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
11-22 11:57:05.600: E/AndroidRuntime(1435): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)

编辑: 数据库与Dalvic虚拟机完美配合。

这是DbHelper的代码:

// versione iniziale
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
public SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    //SOME INSERTS.......

    @Override
    public void onCreate(SQLiteDatabase db) {
        //SOME ON CREATE........

        insert();

        //some updates...




    }



    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //some drop table.....
        onCreate(db);

    }

}

public database_zappapp(Context c) {
    ourContext = c;
}

public database_zappapp open() throws SQLException {

    ourHelper = new DbHelper(ourContext);    <--------- The error is here!!
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {

    ourHelper.close();

}

2 个答案:

答案 0 :(得分:1)

我有错误: java.lang.VerifyError:拒绝类com.ford.syncV4.android.module.ModuleTest,因为编译时验证失败('com.ford.syncV4.android.module.ModuleTest'的声明出现在/ data / app / com中.ford.syncV4.android-1 / base.apk)

顺便说一下,这个错误发生在Android OS 5.0.1上。

对我来说有什么不妥的是:

try {
   synchronized (this) {
      this.wait(100);
   }
} catch (InterruptedException e) {
   /* Exception message can be captured here */
}

!! try / catch块内的同步块!!

当我在synchronized块中设置try / catch时 - 应用程序开始像往常一样工作。顺便说一句,在Android OS 5.0.1之前,这种方法没有问题。我认为这是因为新的ART编译器。

所以,正确的代码看起来像这样:

synchronized (this) {
   try {
      this.wait(testActionItem.getDelay());
   } catch (InterruptedException e) {
      /* Exception message can be captured here */
   }
}

此外,Android问题跟踪器中出现了一个问题: Issue 80961

答案 1 :(得分:-1)

public class DBHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "t.db";
    private static final int DATABASE_VERSION = 1;

    private final String CTEATE_TABLE_USER_INFO = "CREATE TABLE" + UserProfile.Users.TABLE_USER_INFO + "(" + UserProfile.Users._ID + "INTEGER PRIMARY KEY," + UserProfile.Users.TABLE_USER_INFO_COL_USERNAME + "TEXT," + UserProfile.Users.TABLE_USER_INFO_COL_DATE_OF_BIRTH + "TEXT," + UserProfile.Users.TABLE_USER_INFO_COL_GENDER + "TEXT," + UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD + "TEXT)";
    private final String DROP_TABLE_USER_INFO = "DROP TABLE IF EXISTS" + UserProfile.Users.TABLE_USER_INFO;

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CTEATE_TABLE_USER_INFO);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(DROP_TABLE_USER_INFO);
        onCreate(db);
    }

    public boolean addInfor(String username, String dateOfBirth, String gender, String password) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(UserProfile.Users.TABLE_USER_INFO_COL_USERNAME, username);
        contentValues.put(UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD, password);
        contentValues.put(UserProfile.Users.TABLE_USER_INFO_COL_DATE_OF_BIRTH, dateOfBirth);
        contentValues.put(UserProfile.Users.TABLE_USER_INFO_COL_GENDER, gender);

        long row = db.insert(UserProfile.Users.TABLE_USER_INFO, null, contentValues);
        db.close();
        if (row == 1)
            return false;
        else
            return true;

    }

    public boolean upgradeInfor(String username, String dateOfBirth, String gender, String password) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put(UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD, password);
        contentValues.put(UserProfile.Users.TABLE_USER_INFO_COL_DATE_OF_BIRTH, dateOfBirth);
        contentValues.put(UserProfile.Users.TABLE_USER_INFO_COL_GENDER, gender);

        String whereClause = UserProfile.Users.TABLE_USER_INFO_COL_USERNAME + "=?";
        String[] whereClauseArgs = {username};

        int i;
        i = db.update(UserProfile.Users.TABLE_USER_INFO, contentValues, whereClause, whereClauseArgs);

        if (i == 1)
            return true;
        else
            return false;
    }
    public Collection<User> readAllInfor(){
        SQLiteDatabase db=this.getReadableDatabase();

        Cursor result=db.query(UserProfile.Users.TABLE_USER_INFO,null,null,null,null,null,null);

        if(result.getCount()==0){
            result.close();
            db.close();
            return null;
        }
        User user;
        Collection<User> userList=new ArrayList<>();
        while(result.moveToNext()){
            user=new User(result.getInt(result.getColumnIndexOrThrow(UserProfile.Users._ID)), result.getString(result.getColumnIndexOrThrow
                    (UserProfile.Users.TABLE_USER_INFO_COL_USERNAME)), result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD)),
                    result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_DATE_OF_BIRTH)),
                    result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_DATE_OF_BIRTH));
            userList.add(user);

        }
        result.close();
        db.close();
        return userList;
    }
    public User readAllInfor(String username) {
        SQLiteDatabase db = this.getReadableDatabase();

        String whereClause = UserProfile.Users.TABLE_USER_INFO_COL_USERNAME + "=?";

        Cursor result = db.query(UserProfile.Users.TABLE_USER_INFO, null, whereClause, new String[] {username}, null, null, null);

        if(result.getCount() == 0) {
            db.close();
            result.close();
            return null;
        }

        User user = null;
        while(result.moveToNext()) {
            if(result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_USERNAME)).equals(username)) {
                user = new User(result.getInt(result.getColumnIndexOrThrow(UserProfile.Users._ID)),
                        username,
                        result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_DATE_OF_BIRTH)),
                        result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD)),
                        result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_GENDER)));
            }
        }

        db.close();
        result.close();
        return user;
    }
    public boolean deleteInfo(String username) {
        SQLiteDatabase db = this.getWritableDatabase();

        String whereClause = UserProfile.Users.TABLE_USER_INFO_COL_USERNAME + " = ?";
        String[] whereClauseArgs = {username};

        int i;
        i = db.delete(UserProfile.Users.TABLE_USER_INFO, whereClause, whereClauseArgs);

        db.close();
        if(i == 1)
            return true;
        else
            return false;
    }
    public boolean login(String username, String password) {
        SQLiteDatabase db = this.getReadableDatabase();

        String[] columns = {UserProfile.Users.TABLE_USER_INFO_COL_USERNAME, UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD};
        String whereClause = UserProfile.Users.TABLE_USER_INFO_COL_USERNAME + "=? AND " + UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD + "=?";

        Cursor result = db.query(UserProfile.Users.TABLE_USER_INFO, columns, whereClause, new String[] {username, password}, null, null, null);

        boolean bool = false;
        while(result.moveToNext()) {
            if(result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_USERNAME)).equals(username) && result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD)).equals(password)) {
                bool = true;
                break;
            }
        }

        return bool;
    }
}