IllegalArgumentException:版本必须为> = 1,为0

时间:2013-12-25 03:30:26

标签: java android sqlite android-sqlite fatal-error

我收到了"IllegalArgumentException: Version must be >= 1, was 0"错误:

super(context, name, factory, version, errorHandler);

在下面的课程中,我不确定为什么会发生这种情况(我只是想在我的sqlite数据库中添加用户名和密码 - 仅此而已)

12-25 11:28:05.898: E/AndroidRuntime(31438): FATAL EXCEPTION: main
12-25 11:28:05.898: E/AndroidRuntime(31438): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.idg.voiscphone/com.idg.voiscphone.Activity1}: java.lang.IllegalArgumentException: Version must be >= 1, was 0
12-25 11:28:05.898: E/AndroidRuntime(31438):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at android.app.ActivityThread.access$600(ActivityThread.java:156)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at android.os.Looper.loop(Looper.java:153)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at android.app.ActivityThread.main(ActivityThread.java:5297)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at java.lang.reflect.Method.invokeNative(Native Method)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at java.lang.reflect.Method.invoke(Method.java:511)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at dalvik.system.NativeStart.main(Native Method)
12-25 11:28:05.898: E/AndroidRuntime(31438): Caused by: java.lang.IllegalArgumentException: Version must be >= 1, was 0
12-25 11:28:05.898: E/AndroidRuntime(31438):    at android.database.sqlite.SQLiteOpenHelper.<init>(SQLiteOpenHelper.java:100)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at com.idg.voiscphone.AddUser.<init>(AddUser.java:27)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at com.idg.voiscphone.Activity1.onCreate(Activity1.java:48)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at android.app.Activity.performCreate(Activity.java:5262)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
12-25 11:28:05.898: E/AndroidRuntime(31438):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
12-25 11:28:05.898: E/AndroidRuntime(31438):    ... 11 more

JAVA:

public class AddUser extends SQLiteOpenHelper {

    public AddUser(Context context, String name, CursorFactory factory,
            int version, DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
        // TODO Auto-generated constructor stub
    }

    private static final String tableName = "USER_RECORDS";

    public void onCreate(SQLiteDatabase database) {

        String insertSQL = "INSERT INTO " + tableName + " (" + "uname"
                + "pword" + ");";
        Log.d("addRecord()", insertSQL);
        SQLiteDatabase dataBase = this.getWritableDatabase();
        dataBase.execSQL(insertSQL);
        dataBase.close();

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }
}

1 个答案:

答案 0 :(得分:0)

IllegalArgumentException: Version must be >= 1, was 0

版本为1或大于1.在您的情况下为0.

此外,你需要为构造函数

使用正确的参数
new AddUser(context,name,factory,version,errorHandler);

您也可以使用以下内容。

 private static final String DATABASE_NAME = "RECORDS";
 private static final int DATABASE_VERSION = 1;
 // Have the above constants
 public AddUser(Context context) {
    super(context,DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
 }

使用的构造函数是

SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)

更多信息

http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

活动

AddUser ad= new AddUser(ActivityName.this);