ANDROID:引起:java.lang.reflect.InvocationTargetException

时间:2016-02-01 21:28:58

标签: java android database sqlite

我尝试将edittext中的数据插入到数据库中,但是我得到了这些错误并无法解决。我创建了DBHelper,其代码如下,就像在DB中添加数据的类一样。也是构造函数和getter以及setter。我真的需要一些帮助。

02-01 22:22:07.180 23428-23428/? E/SQLiteLog: (1) AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
02-01 22:22:07.200 23428-23428/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x42020540)
02-01 22:22:07.380 23428-23428/? E/AndroidRuntime: FATAL EXCEPTION: main
                                 java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:4102)
at android.view.View$PerformClick.run(View.java:17085)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5520)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:4102) 
at android.view.View$PerformClick.run(View.java:17085) 
at android.os.Handler.handleCallback(Handler.java:615) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:155) 
at android.app.ActivityThread.main(ActivityThread.java:5520) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1): , while compiling: CREATE TABLE Logs(_idINTEGER PRIMARY KEY AUTOINCREMENT,titleTEXT,plate_numberTEXT,sort_idTEXT,gradeTEXT,diameterTEXT,lengthTEXT,survey_idINTEGER AUTOINCREMENT);
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:909)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:520)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1719)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1650)
at com.example.matija.ams.LogsDBHandler.onCreate(LogsDBHandler.java:51)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.example.matija.ams.LogsDBHandler.addLogs(LogsDBHandler.java:62)
at com.example.matija.ams.AddLogs.saveButtonClicked(AddLogs.java:55)
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
at android.view.View.performClick(View.java:4102) 
at android.view.View$PerformClick.run(View.java:17085) 
at android.os.Handler.handleCallback(Handler.java:615) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:155) 
at android.app.ActivityThread.main(ActivityThread.java:5520) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796) 
at dalvik.system.NativeStart.main(Native Method) 
02-01 22:22:09.240 23428-23428/? D/Process: killProcess, pid=23428
02-01 22:22:09.270 23428-23428/? D/Process: dalvik.system.VMStack.getThreadStackTrace(Native Method)
02-01 22:22:09.270 23428-23428/? D/Process: java.lang.Thread.getStackTrace(Thread.java:599)
02-01 22:22:09.270 23428-23428/? D/Process: android.os.Process.killProcess(Process.java:956)
02-01 22:22:09.270 23428-23428/? D/Process: com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108)
02-01 22:22:09.270 23428-23428/? D/Process: java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
02-01 22:22:09.270 23428-23428/? D/Process: java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
02-01 22:22:09.270 23428-23428/? D/Process: dalvik.system.NativeStart.main(Native Method)

这是我的DbHandler和add方法

public class LogsDBHandler extends SQLiteOpenHelper {

    //DB version
    private static final int DATABASE_VERSION = 11;
    // DB name
    private static final String DATABASE_NAME = "Log";

    //table name
    public static final String TABLE_LOGS = "Logs";
    public static final String TABLE_SURVEY = "SURVEY";

    //TableLogs column names
    public static final String KEY_ID = "_id";
    public static final String KEY_TITLE = "title";
    public static final String KEY_PLATE_NUMBER = "plate_number";
    public static final String KEY_SORTID = "sort_id";
    public static final String KEY_GRADE = "grade";
    public static final String KEY_DIAMETER = "diameter";
    public static final String KEY_LENGTH = "length";
    public static final String KEY_SURVEYID = "survey_id";

    //TableSurvey column names
    public static final String KEY_CREATEDAT = "created_at";
    public static final String KEY_SURVEY_TITLE = "survey_title";
    public static final String KEY_STATE = "state";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_LOGS_TABLE = "CREATE TABLE " + TABLE_LOGS + "(" +
                KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," +
                KEY_TITLE + "TEXT," + KEY_PLATE_NUMBER + "TEXT," +
                KEY_SORTID + "TEXT," + KEY_GRADE + "TEXT," +
                KEY_DIAMETER + "TEXT," + KEY_LENGTH + "TEXT," +
                KEY_SURVEYID + "INTEGER AUTOINCREMENT" +");";
        db.execSQL(CREATE_LOGS_TABLE);
    }

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

    //add new row
    public void addLogs(Logs log) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_TITLE, log.get_title());
        values.put(KEY_PLATE_NUMBER, log.get_plate_number());
        values.put(KEY_SORTID, log.get_sort_id());
        values.put(KEY_GRADE, log.get_grade());
        values.put(KEY_DIAMETER, log.get_diameter());
        values.put(KEY_LENGTH, log.get_length());
        db.insert(TABLE_LOGS, null, values);
        db.close();
    }
}

所以下一个代码是用于将edittext中的数据添加到数据库中。如果有什么不对的地方请告诉我。

public class AddLogs extends AppCompatActivity {

    private EditText title;
    private EditText plate_number;
    private Spinner sort_id;
    private Spinner grade;
    private EditText diameter;
    private EditText length;
    LogsDBHandler dbHandler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_logs);

        final Button changeActivityButton1 = (Button) findViewById(R.id.btn_back);
        changeActivityButton1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View aView) {
                Intent toAnotherActivity = new Intent(aView.getContext(), MainActivity.class);
                startActivityForResult(toAnotherActivity, 0);
            }
        });

        title = (EditText) findViewById(R.id.inputTitle);
        plate_number = (EditText) findViewById(R.id.supplierNumberInput);
        sort_id = (Spinner) findViewById(R.id.spinnerSortInput);
        grade = (Spinner) findViewById(R.id.spinnerClassInput);
        diameter = (EditText) findViewById(R.id.diameterInput);
        length = (EditText) findViewById(R.id.lengthInput);
        dbHandler = new LogsDBHandler(this, null, null, 1);

    }

        public void saveButtonClicked(View view) {
            Logs log = new Logs(title.getText().toString(), plate_number.getText().toString(),
                    sort_id.getSelectedItem().toString(), grade.getSelectedItem().toString(),
                    diameter.getText().toString(), length.getText().toString());
            dbHandler.addLogs(log);
            Toast.makeText(getBaseContext(), "Spremljeno", Toast.LENGTH_SHORT ).show();

       }
}

所以这就是所有代码(我没有把代码放在构造函数,getter和setter中)。所以我有人知道问题是什么,请说和写。感谢。

1 个答案:

答案 0 :(得分:0)

您的SQL格式不正确。列名称与其余属性之间没有空格。这就是异常根本原因是:

Caused by: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1): , while compiling: CREATE TABLE Logs(_idINTEGER PRIMARY KEY AUTOINCREMENT,titleTEXT,plate_numberTEXT,sort_idTEXT,gradeTEXT,diameterTEXT,lengthTEXT,survey_idINTEGER AUTOINCREMENT);

看看你的onCreate回调:

String CREATE_LOGS_TABLE = "CREATE TABLE " + TABLE_LOGS + "(" +
            KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," +
            KEY_TITLE + "TEXT," + KEY_PLATE_NUMBER + "TEXT," +
            KEY_SORTID + "TEXT," + KEY_GRADE + "TEXT," +
            KEY_DIAMETER + "TEXT," + KEY_LENGTH + "TEXT," +
            KEY_SURVEYID + "INTEGER AUTOINCREMENT" +");";

如果要手动构建SQL字符串,而不是使用诸如Flyway之类的迁移库,则需要确保根据标准格式化SQL。