Android Studio:java.lang.RuntimeException:无法启动活动ComponentInfo ...:java.lang.IllegalArgumentException:column' _id'不存在

时间:2016-06-11 12:08:03

标签: android

完整错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.myfavoriteplaces.myfavoriteplaces, PID: 32383
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myfavoriteplaces.myfavoriteplaces/com.myfavoriteplaces.myfavoriteplaces.ListPlaces}: java.lang.IllegalArgumentException: column '_id' does not exist
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
                                                   at android.app.ActivityThread.access$900(ActivityThread.java:177)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:135)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5910)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
                                                Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
                                                   at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
                                                   at android.widget.CursorAdapter.init(CursorAdapter.java:172)
                                                   at android.widget.CursorAdapter.<init>(CursorAdapter.java:149)
                                                   at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
                                                   at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
                                                   at com.myfavoriteplaces.myfavoriteplaces.ListPlaces.displayListView(ListPlaces.java:49)
                                                   at com.myfavoriteplaces.myfavoriteplaces.ListPlaces.onCreate(ListPlaces.java:35)
                                                   at android.app.Activity.performCreate(Activity.java:6178)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769) 
                                                   at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:135) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5910) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

我的代码类BD:

package com.myfavoriteplaces.myfavoriteplaces;

public class BD {
    private int _id;
    private String nom_place;
    private String type_place;
    private String address_place;
    private String description_place;

    public BD(int id, String nom, String type, String address, String description){
        this._id = id;
        this.nom_place = nom;
        this.type_place = type;
        this.address_place = address;
        this.description_place = description;
    }

    public int getId_place(){ return _id; }

    public void setId_place(int id){
        this._id = id;
    }

    public String getNom_place(){
        return nom_place;
    }

    public void setNom_place(String nom){
        this.nom_place = nom;
    }

    public String getType_place(){
        return type_place;
    }

    public void setType_place(String type){
        this.type_place = type;
    }

    public String getAddress_place(){
        return address_place;
    }

    public void setAddress_place(String address){
        this.address_place = address;
    }

    public String getDescription_place(){
        return description_place;
    }

    public void setDescription_place(String description){
        this.description_place = description;
    }
}

BDManager类:

package com.myfavoriteplaces.myfavoriteplaces;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class BDManager {
    private static final String TABLE_NAME = "places";
    public static final String KEY_ID_PLACE = "_id";
    public static final String KEY_NOM_PLACE = "nom_place";
    public static final String KEY_TYPE_PLACE = "type_place";
    public static final String KEY_ADDRESS_PLACE = "address_place";
    public static final String KEY_DESCRIPTION_PLACE = "description_place";
    public static final String CREATE_TABLE_PLACES = "CREATE TABLE              "+TABLE_NAME+" " +
        " (" +
        " "+KEY_ID_PLACE+" INTEGER primary key autoincrement," +
        " "+KEY_NOM_PLACE+" TEXT" +
        " "+KEY_TYPE_PLACE+" TEXT" +
        " "+KEY_ADDRESS_PLACE+" TEXT" +
        " "+KEY_DESCRIPTION_PLACE+" TEXT" +
        ");";

    private MySQLite maBaseSQLite;
    private SQLiteDatabase db;

    public BDManager(Context context){
        maBaseSQLite = MySQLite.getInstance(context);
    }

    public void open() {
        db = maBaseSQLite.getWritableDatabase();
    }

    public void close() {
        db.close();
    }

    public long addPlace(BD place){
        ContentValues values = new ContentValues();
        values.put(KEY_NOM_PLACE, place.getNom_place());
        values.put(KEY_TYPE_PLACE, place.getType_place());
        values.put(KEY_ADDRESS_PLACE, place.getAddress_place());
        values.put(KEY_DESCRIPTION_PLACE, place.getDescription_place());

        return db.insert(TABLE_NAME,null,values);
    }

    public int modPlace(BD place){
        ContentValues values = new ContentValues();
        values.put(KEY_NOM_PLACE, place.getNom_place());
        values.put(KEY_TYPE_PLACE, place.getType_place());
        values.put(KEY_ADDRESS_PLACE, place.getAddress_place());
        values.put(KEY_DESCRIPTION_PLACE, place.getDescription_place());

        String where = KEY_ID_PLACE+" = ?";
        String[] whereArgs = {place.getId_place()+""};

        return db.update(TABLE_NAME, values, where, whereArgs);
    }

    public BD getPlace(int id){
        BD p = new BD(0,"","","","");

        Cursor c = db.rawQuery("SELECT * FROM "+TABLE_NAME+" WHERE "+KEY_ID_PLACE+"="+id, null);
        if (c.moveToFirst()){
            p.setId_place(c.getInt(c.getColumnIndex(KEY_ID_PLACE)));
            p.setNom_place(c.getString(c.getColumnIndex(KEY_NOM_PLACE)));
            p.setType_place(c.getString(c.getColumnIndex(KEY_TYPE_PLACE)));
                  p.setAddress_place(c.getString(c.getColumnIndex(KEY_ADDRESS_PLACE)));
          p.setDescription_place(c.getString(c.getColumnIndex(KEY_DESCRIPTION_PLACE)));

            c.close();
        }
        return p;
    }

    public Cursor getPlaces(){
        return db.rawQuery("SELECT * FROM "+TABLE_NAME, null);
    }
    }

我完全不知道出了什么问题,花了几个小时试图修复它但它仍在崩溃。错误谈论&#34; _id&#34;但我添加它仍然无法正常工作......

1 个答案:

答案 0 :(得分:0)

当我尝试打开此页面时崩溃了。在这个页面中,我只想将数据库中的所有名称放置到列表视图中。

package com.myfavoriteplaces.myfavoriteplaces;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class ListPlaces extends AppCompatActivity {

    ListView mListView;
    BDManager sav;
    SimpleCursorAdapter dataAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_places);

        mListView = (ListView) findViewById(R.id.affichage_listplace);

        sav = new BDManager(this);
        sav.open();
        sav.getPlaces();

        displayListView();
    }

    private void displayListView(){
        Cursor cursor = sav.getPlaces();

        String[] columns = new String[]{
                BDManager.KEY_NOM_PLACE
        };

        int[] to = new int[] {
                R.id.NomPlace
        };

        dataAdapter = new SimpleCursorAdapter(
                this, R.layout.list_places,
                cursor,
                columns,
                to,
                0
        );

        mListView = (ListView) findViewById(R.id.affichage_listplace);
        mListView.setAdapter(dataAdapter);
    }
}