dimanche 2 août 2015

Caused by: java.lang.IllegalArgumentException: column '_id' does not exist still Unsolved

here i am a newbie in Android Programming, i want to make an activity that display database in listview but when i run thats activity i got some error like this :

Caused by: java.lang.IllegalArgumentException: column '_id' does not exist

I have searching on google and on Stackoverflow, i found some answer that to said

The Cursor must include a column named _id or this class will not work. You can maybe try to fake it using your existing ID

i tried that way but now i have some another error that said

Caused by: android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT CategName, _id FROM category_table

I don't know what exactly happen to this error but i wish someone could help me.. Please master gimme some explanation with easy understanding words. Thank You...

NB. this is my activity where i got those error

public class CategorySetting extends ListActivity { private SQLiteDatabase db;

private CursorAdapter currAdapter;
private static Button BtnIAddCateg;
private static Button BtnICancelCateg;
private static final String TAG = CategorySetting.class.getSimpleName();
DatabaseHelper dBHelper = new DatabaseHelper (this);
private ListView list;

private String tableName = DatabaseHelper.TABLE_Categ_NAME;
private String columnName = DatabaseHelper.COL2;
private CursorAdapter data_sumber;
private static final String kolom[] = {"CategName"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   // setContentView(R.layout.activity_category_setting);
    //onButtonClickButtonListener();
    ListView list = getListView();
   // list = (ListView)findViewById(R.id.list);

    ListView view = getListView();
    view.addHeaderView(getLayoutInflater().inflate(R.layout.row, null));
    db = dBHelper.getWritableDatabase();
    this.muat_ulang();


}


public void muat_ulang()
{
    Cursor data = db.query( tableName, kolom, null, null, null, null,null);
    data_sumber = new SimpleCursorAdapter(this, R.layout.row, data, kolom,new int[] { R.id.list });
    setListAdapter(data_sumber);
}


/*public void reload(){
    try {
        DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
        db = dbHelper.getWritableDatabase();
        Cursor c = db.rawQuery("SELECT CategName FROM " + tableName, null);
        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    String categName = c.getString(c.getColumnIndex("CategName"));
                }while (c.moveToNext());
            }
        }
    } catch (SQLiteException se ) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
    } finally {
        if (db != null)
            db.execSQL("DELETE FROM " + tableName);
        db.close();
    }

}*/



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_category_setting, menu);
    return true;
}

public void onButtonClickButtonListener(){

    BtnIAddCateg = (Button)findViewById(R.id.btnAddNewCateg);
    BtnIAddCateg.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intentAddCateg = new Intent ("com.example.ever_ncn.cashflow.AddCategory");
                    startActivity(intentAddCateg);
                }
            }
    );

    BtnICancelCateg = (Button)findViewById(R.id.btnCancelCateg);
    BtnICancelCateg.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                finish();
                }
            }
    );
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Aucun commentaire:

Enregistrer un commentaire