应用程序崩溃在启动由于java.lang.IllegalArgumentException:列'_id'不存在 [英] App Crashes On Startup Due To java.lang.IllegalArgumentException: column '_id' does not exist

查看:152
本文介绍了应用程序崩溃在启动由于java.lang.IllegalArgumentException:列'_id'不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我启动我的应用程序,我得到一个 java.lang.IllegalArgumentException:列'_id'不存在错误在我的LogCat。我创建了列'_ id',但它仍然抛出这个。这是我的主要.java:

Whenever I start my app, I get a java.lang.IllegalArgumentException: column '_id' does not exist error in my LogCat. I have created the column '_id', but it still throws this. Here is my main .java:

package com.gantt.shoppinglist;

import android.app.Dialog;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ShoppingList extends ListActivity {

    private DataHelper DataHelper;
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DataHelper = new DataHelper(this);

        Cursor c = (Cursor) DataHelper.selectAll();
        long id = c.getLong(c.getColumnIndex("_id"));
        startManagingCursor(c);

        ListView lv = (ListView) findViewById(android.R.id.list);

        String[] from = new String[] { com.gantt.shoppinglist.DataHelper.getDatabaseName() };
        int[] to = new int[] { android.R.id.text1 };

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, c, from, to);       
        lv.setAdapter(adapter);

        Button button1main = (Button) findViewById(R.id.add);
        button1main.setOnClickListener(new OnClickListener()  {
            @Override
            public void onClick(View v)  {
            final Dialog additem = new Dialog(ShoppingList.this);
            additem.setContentView(R.layout.maindialog);
            final EditText et = (EditText)additem.findViewById(R.id.edittext);
            additem.setTitle("Type your item");
            additem.setCancelable(true);
            et.setHint("Type the name of an item...");

            Button button = (Button) additem.findViewById(R.id.cancel);
            button.setOnClickListener(new OnClickListener()  {
                @Override
                public void onClick(View v)  {
                    additem.dismiss();
                }
            });
            additem.show();

            Button ok = (Button) additem.findViewById(R.id.ok);
            ok.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    final String text = et.getText().toString();
                    additem.dismiss();
                    et.setText("");
                }
            });
       }
        });
    }
}

这是我的DataHelper类:

Here is my DataHelper class:

package com.gantt.shoppinglist;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class DataHelper {

       private static final String DATABASE_NAME = "items.db";
       private static final int DATABASE_VERSION = 1;
       private static final String TABLE_NAME = "table1";
       public static final String KEY_ROWID = "_id";

       private Context context;
       private SQLiteDatabase db;

       private SQLiteStatement insertStmt;
       private static final String INSERT = "insert into " 
          + TABLE_NAME + "(name) values (?)";

       public DataHelper(Context context) {
          this.context = context;
          OpenHelper openHelper = new OpenHelper(this.context);
          this.db = openHelper.getWritableDatabase();
          this.insertStmt = this.db.compileStatement(INSERT);
       }

       public long insert(String name) {
          this.insertStmt.bindString(1, name);
          return this.insertStmt.executeInsert();
       }

       public void deleteAll() {
          this.db.delete(TABLE_NAME, null, null);
       }

       public Cursor selectAll() {
          List<String> list = new ArrayList<String>();
          Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" }, 
            null, null, null, null, "name desc");
          if (cursor.moveToFirst()) {
             do {
                list.add(cursor.getString(0)); 
             } while (cursor.moveToNext());
          }
          if (cursor != null && !cursor.isClosed()) {
             cursor.close();
          }
          return cursor;
       }

       public static String getDatabaseName() {
        return DATABASE_NAME;
    }

    private static class OpenHelper extends SQLiteOpenHelper {

          OpenHelper(Context context) {
             super(context, getDatabaseName(), null, DATABASE_VERSION);
          }

          @Override
          public void onCreate(SQLiteDatabase db) {
              db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT");

          }

          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
             Log.w("Example", "Upgrading database, this will drop tables and recreate.");
             db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
             onCreate(db);
          }
       }
    }


推荐答案

类似的问题 - 我认为这是一个必须选择(或选择)的东西称为 _id 的情况,因为 SimpleCursorAdapter 需要它。

I had a similar issue - I think it's a case of having to 'select' (or 'select as') something called _id because the SimpleCursorAdapter needs it.

文档


处理内容URI ID

按照惯例,提供程序通过
访问表中的单个行,接受一个内容URI,该行的ID值位于$结束处b $ b URI。按照惯例,提供者将ID值与表的
_ID 列匹配,并对匹配的行执行请求的访问。

By convention, providers offer access to a single row in a table by accepting a content URI with an ID value for the row at the end of the URI. Also by convention, providers match the ID value to the table's _ID column, and perform the requested access against the row that matches.

这个约定方便了访问
a提供程序的应用程序的通用设计模式。应用程序对提供程序执行查询,并在 ListView 中显示
产生 Cursor c $ c> CursorAdapter 。 CursorAdapter 的定义
需要 Cursor 中的一个列为 _ID

This convention facilitates a common design pattern for apps accessing a provider. The app does a query against the provider and displays the resulting Cursor in a ListView using a CursorAdapter. The definition of CursorAdapter requires one of the columns in the Cursor to be _ID.

在我的例子中,我在表中有一个自动编号列,名为'oid'改变我的SELECT命令为(例如)...

In my case, I have an autonumber column in my table called 'oid' so I altered my SELECT command to be (for example)...

SELECT oid as _id, name, number FROM mytable

这解决了我的问题。

显示更多的代码...

EDIT to show more extensive code...

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

    GridView channel_selector_grid = (GridView) findViewById(R.id.channel_grid);
    sca = getGuideAdapter();
    channel_selector_grid.setAdapter(sca);
}

public SimpleCursorAdapter getGuideAdapter() {
    SimpleCursorAdapter adapter = null;
    SQLiteDatabase db = SQLiteDatabaseHelper.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT DISTINCT oid as _id, name, number FROM CHAN_TABLE ORDER BY number", null);
    if (cursor.moveToFirst()) {
        String[] columnNames = { "name" };
        int[] resIds = { R.id.channel_name };
        adapter = new SimpleCursorAdapter(this, R.layout.channel_selector_item, cursor, columnNames, resIds);
    }
    return adapter; 
}

这篇关于应用程序崩溃在启动由于java.lang.IllegalArgumentException:列'_id'不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆