簡體   English   中英

java.lang.IllegalArgumentException:列'_id'不存在-UploadActivity

[英]java.lang.IllegalArgumentException: column '_id' does not exist - UploadActivity

我寫了一個小程序,我在其中接受用戶的數據並將其存儲到數據庫中。 要按用戶接受數據,我在DialogBox中使用EditText(s)。

現在,我正在嘗試從數據庫中獲取數據,並需要在創建對話框時將該數據顯示到DialogBox中。

UploadActivity.java:

@Override
        public Dialog onCreateDialog(int id) {

            helper = new DBHelper(this);

            AlertDialog dialogDetails = null;
            switch (id) {
            case DIALOG_LOGIN:
                LayoutInflater inflater = LayoutInflater.from(this);
                View dialogview = inflater.inflate(R.layout.dialog_layout, null);
                AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
                dialogbuilder.setTitle("Image Information");
                dialogbuilder.setView(dialogview);
                dialogDetails = dialogbuilder.create();
                break;  
            }
            return dialogDetails;
        }

        @Override
        public void onPrepareDialog(int id, Dialog dialog) {
            switch (id) {
            case DIALOG_LOGIN:


            final AlertDialog alertDialog = (AlertDialog) dialog;
                Button uploadButton = (Button) alertDialog
                        .findViewById(R.id.btnUploadData);
                Button saveButton = (Button) alertDialog
                        .findViewById(R.id.btnSaveXML);
                Button cancelButton = (Button) alertDialog
                        .findViewById(R.id.btnCancelDialog);

                editImageName = (EditText)alertDialog
                        .findViewById(R.id.editImageName);
                editPersonName = (EditText) alertDialog
                        .findViewById(R.id.editPersonName);
                editPersonaEmail = (EditText) alertDialog
                        .findViewById(R.id.editPersonEmail);
                editPersonTelephone = (EditText) alertDialog
                        .findViewById(R.id.editPersonTelephone);

                editImageName.setText(fileName);

                fetchData();


    saveButton.setOnClickListener(new View.OnClickListener() {
                    @Override                   
                        public void onClick(View v) {   

                        ContentValues values = new ContentValues();
                        values.put(DBHelper.IMAGE_NAME, editImageName.getText().toString());
                        values.put(DBHelper.PERSON_NAME, editPersonName.getText().toString());
                        values.put(DBHelper.PERSON_EMAIL, editPersonaEmail.getText().toString());
                        values.put(DBHelper.PERSON_PHONE, editPersonTelephone.getText().toString());

                        // Call insert method of SQLiteDatabase Class and close after
                        // performing task
                        db = helper.getWritableDatabase();
                        db.insert(DBHelper.TABLE_NAME, null, values);
                        db.close();

                        Toast.makeText(UploadActivity.this, "Stored to Database",
                                Toast.LENGTH_LONG).show();


                    }
                });     

                cancelButton.setOnClickListener(new View.OnClickListener(){ 
                    @Override
                        public void onClick(View v) {

                        alertDialog.dismiss();
                        }
                });
            }
        }

        // Fetch Fresh data from database and display into listview
        @SuppressWarnings("deprecation")
        private void fetchData() {
            db = helper.getReadableDatabase();
            Cursor c = db.query(DBHelper.TABLE_NAME, null, null, null, null, null, null);
            adapter = new SimpleCursorAdapter(
                    this,
                    R.layout.dialog_layout,
                    c,
                    new String[] { DBHelper.IMAGE_NAME, DBHelper.PERSON_NAME,
                            DBHelper.PERSON_EMAIL, DBHelper.PERSON_PHONE },
                    new int[] { R.id.editImageName, R.id.editPersonName, R.id.editPersonEmail, R.id.editPersonTelephone });

        }

DBHelper.java:

public class DBHelper extends SQLiteOpenHelper {

    // Static Final Variable database meta information

    static final String DATABASE_NAME = "MyDatabase.db";
    static final int VERSION = 1;
    static final String TABLE_NAME = "MyTable";

    static final String IMAGE_CODE = "imageCode";
    static final String IMAGE_NAME = "imageName";
    static final String PERSON_NAME = "personName";
    static final String PERSON_EMAIL = "personEmail";
    static final String PERSON_PHONE = "personPhone";

    // Override constructor
    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, VERSION);

    }

    // Override onCreate method
    @Override
    public void onCreate(SQLiteDatabase db) {


        db.execSQL(
                "CREATE TABLE " + TABLE_NAME + 
                " ( " + IMAGE_CODE + " INTEGER PRIMARY KEY AUTOINCREMENT, "   // Auto Increment
                        + IMAGE_NAME + " text, "    // Image Name
                        + PERSON_NAME + " text, "   // Person Name
                        + PERSON_EMAIL + " text, "  // Person Email
                        + PERSON_PHONE + " text )"); // Person Phone
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        // Drop old version table
        db.execSQL("Drop table " + TABLE_NAME);  
        // Create New Version table
        onCreate(db);
    }

}

我通過在列表中點擊數據按鈕來調用Dialog ...

           // Data
    final Button btnData = (Button) convertView.findViewById(R.id.btnData);
        btnData.setTextColor(Color.BLACK);
        btnData.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
              public void onClick(View v) {
                fileName=ImageList.get(position).toString().substring(strPath.lastIndexOf('/')+1, strPath.length());
        showDialog(DIALOG_LOGIN);
            }
        }); 

日志報告:-

 11-23 05:19:57.953: W/dalvikvm(1475): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-23 05:19:57.984: E/AndroidRuntime(1475): FATAL EXCEPTION: main
11-23 05:19:57.984: E/AndroidRuntime(1475): java.lang.IllegalArgumentException: column '_id' does not exist
11-23 05:19:57.984: E/AndroidRuntime(1475):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at android.widget.CursorAdapter.init(CursorAdapter.java:168)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at android.widget.CursorAdapter.<init>(CursorAdapter.java:116)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:52)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at com.example.db.UploadActivity.fetchData(UploadActivity.java:416)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at com.example.db.UploadActivity.onPrepareDialog(UploadActivity.java:284)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at android.app.Activity.onPrepareDialog(Activity.java:3027)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at android.app.Activity.showDialog(Activity.java:3090)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at android.app.Activity.showDialog(Activity.java:3041)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at com.example.db.UploadActivity$ImageAdapter$2.onClick(UploadActivity.java:222)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at android.view.View.performClick(View.java:4240)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at android.view.View$PerformClick.run(View.java:17721)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at android.os.Handler.handleCallback(Handler.java:730)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at android.os.Looper.loop(Looper.java:137)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at java.lang.reflect.Method.invokeNative(Native Method)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at java.lang.reflect.Method.invoke(Method.java:525)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-23 05:19:57.984: E/AndroidRuntime(1475):     at dalvik.system.NativeStart.main(Native Method)

行號416是:

 new int[] { R.id.editImageName, R.id.editPersonName, R.id.editPersonEmail, R.id.editPersonTelephone });

行號284是:

 fetchData();

行號222是:

 showDialog(DIALOG_LOGIN);

如果您從此代碼中得到錯誤,則db = helper.getReadableDatabase(); 這意味着您尚未初始化您的helper並且如果在初始化該helper之后並在插入之時獲取諸如java.lang.IllegalArgumentException: column '_id' does not exist錯誤java.lang.IllegalArgumentException: column '_id' does not exist因為您正試圖使用​​需要一個稱為_id的列的游標。 就像編輯表創建語句並添加名為_id的列一樣簡單。

它的聲明看起來像這樣:

_id INTEGER PRIMARY KEY AUTOINCREMENT

添加它,然后您就可以使用它了。 我相信這是使用SimpleCursorAdapter所必需的要求。

 "CREATE TABLE IF NOT EXISTS TABLE_NAME( _id INTEGER PRIMARY KEY AUTOINCREMENT,  ...);"

在左側假體和_id之間添加一個空格

這要么

helper.getReadableDatabase();

返回null引用或

您尚未初始化您的

helper

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM