本文介绍了柱_id不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的logcat错误就是这样
logcat的
8月8号至29号:20:57.961:ERROR / AndroidRuntime(1766):java.lang.RuntimeException的:无法启动的活动ComponentInfo {one.two / one.two.Booking} :java.lang.IllegalArgumentException:如果列'_id'不存在
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在android.app.ActivityThread.access $ 1800(ActivityThread.java:112)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1692)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在android.os.Handler.dispatchMessage(Handler.java:99)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在android.os.Looper.loop(Looper.java:123)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在android.app.ActivityThread.main(ActivityThread.java:3948)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在java.lang.reflect.Method.invokeNative(本机方法)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在java.lang.reflect.Method.invoke(Method.java:521)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:782)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在dalvik.system.NativeStart.main(本机方法)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):java.lang.IllegalArgumentException异常:产生的原因列'_id'不存在
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在android.widget.CursorAdapter.init(CursorAdapter.java:111)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在android.widget.CursorAdapter< INIT>(CursorAdapter.java:90)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在android.widget.ResourceCursorAdapter< INIT>(ResourceCursorAdapter.java:47)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在android.widget.SimpleCursorAdapter< INIT>(SimpleCursorAdapter.java:85)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在one.two.Booking.onCreate(Booking.java:34)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
八月八日至29日:20:57.961:ERROR / AndroidRuntime(1766):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
我DBAdapter.java的一部分
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
DB =新DBAdapter(本);
db.open();
的setContentView(R.layout.booking);
/ *长的ID;
ID = db.insertTime(
新的String(08:00)); * /
光标spinnerCursor = db.getSpinnerData();
startManagingCursor(spinnerCursor);
微调colourSpinner =(微调)findViewById(R.id.spinner);
创建数据库的
包one.two;
进口java.io.IOException异常;
进口android.content.ContentValues;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.SQLException;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteException;
进口android.database.sqlite.SQLiteOpenHelper;
进口android.util.Log;
公共类DBAdapter
{
私有静态字符串DB_PATH =/data/data/one.two/databases/;
私有静态字符串DB_NAME =irsyad;
//值登录表
公共静态最后弦乐KEY_ROWID =_id;
公共静态最后弦乐KEY_USERNAME =用户名;
公共静态最后弦乐KEY_PASSWORD =密码;
公共静态最后弦乐KEY_LNAME =姓;
公共静态最后弦乐KEY_FNAME =姓;
公共静态最后弦乐KEY_ROWID2 =_id;
公共静态最后弦乐KEY_STATUS =状态;
公共静态最后弦乐KEY_DESTINATION =目的地;
公共静态最后弦乐KEY_ARRIVAL =到来;
公共静态最后弦乐KEY_FERRY =摆渡
私有静态最后字符串变量=DBAdapter;
//声明数据库名,表名
私有静态最后弦乐DATABASE_NAME =irsyad;
私有静态最后弦乐DATABASE_TABLE =用户;
私有静态最后弦乐DATABASE_TABLE_2 =口;
私有静态最终诠释DATABASE_VERSION = 1;
//声明了数据库表中的规则
私有静态最后弦乐DATABASE_CREATE =
创建表的用户(_id整数主键自动增量,
+用户名文本不为空,密码文本不为空,
+姓氏文字不为空,名字文本不为空);;
私有静态最后弦乐DATABASE_CREATE_2 =
创建表的端口(_id整数主键自动增量,
+状态文本空,目标文本空,
+的到来文字空,轮渡文本NULL);;
私人最终上下文的背景下;
私人DatabaseHelper DBHelper;
私人SQLiteDatabase分贝;
公共DBAdapter(上下文CTX)
{
this.context = CTX;
DBHelper =新DatabaseHelper(上下文);
}
私有静态类DatabaseHelper扩展SQLiteOpenHelper
{
DatabaseHelper(上下文的背景下)
{
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
}
//创建表与我们设定的规则。
@覆盖
公共无效的onCreate(SQLiteDatabase DB)
{
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE_2);
}
// OnUpgrade仅用于当u改变数据库的版本2等。
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,
INT动态网页)
{
Log.w(TAG,从版本升级数据库+ oldVersion
+来
+动态网页+,这将销毁所有旧数据);
db.execSQL(DROP TABLE IF EXISTS端口);
的onCreate(DB);
}
}
// ---打开数据库---
公共DBAdapter的open()抛出的SQLException
{
DB = DBHelper.getWritableDatabase();
回到这一点;
}
// ---关闭数据库---
公共无效的close()
{
DBHelper.close();
}
//方法插入登录信息,可用于其他Java文件时DBAdapter是
//在java文件中声明。例如DBAdapter DB =新DBAdapter(本);
众长insertUser(用户名字符串,字符串密码,串名字,字符串姓)
{
ContentValues initialValues =新ContentValues();
initialValues.put(KEY_USERNAME,用户名);
initialValues.put(KEY_PASSWORD,密码);
initialValues.put(KEY_LNAME,姓氏);
initialValues.put(KEY_FNAME,名字);
返回db.insert(DATABASE_TABLE,空,initialValues);
}
/ *众长insertTime(字符串到达)
{
ContentValues初值=新ContentValues();
initialValue.put(KEY_ARRIVAL,到达);
返回db.insert(DATABASE_TABLE_2,KEY_ARRIVAL,初值);
} * /
// ---删除特定的标题---
公共布尔deleteUser(长ROWID)
{
返回db.delete(DATABASE_TABLE,KEY_ROWID +
=+ ROWID,NULL)> 0;
}
//方法从数据库中检索所有的投入
公共光标getAllUser()
{
返回db.query(DATABASE_TABLE,新的String [] {
KEY_ROWID,
KEY_USERNAME,
KEY_PASSWORD,
KEY_LNAME,
KEY_FNAME,},
空值,
空值,
空值,
空值,
空值);
}
公共光标getAllData()
{
返回db.query(DATABASE_TABLE_2,新的String [] {
KEY_ROWID2,
KEY_STATUS,
KEY_DESTINATION,
KEY_ARRIVAL,
KEY_FERRY,},
空值,
空值,
空值,
空值,
空值);
}
// ---检索特定的标题---
公共光标的getUser(长ROWID)抛出的SQLException
{
光标mCursor =
db.query(真,DATABASE_TABLE,新的String [] {
KEY_ROWID,
KEY_USERNAME,
KEY_PASSWORD,
KEY_LNAME,
KEY_FNAME,},
KEY_ROWID +=+ ROWID,
空值,
空值,
空值,
空值,
空值);
如果(mCursor!= NULL){
mCursor.moveToFirst();
}
返回mCursor;
}
公共光标getSpinnerData()抛出的SQLException
{
光标mCursor =
db.query(DATABASE_TABLE_2,新的String [] {
KEY_ARRIVAL,},
空值,
空值,
空值,
空值,
空值);
如果(mCursor!= NULL){
mCursor.moveToFirst();
}
返回mCursor;
}
// ---更新一个标题---
公共布尔UpdateUser两个(长ROWID,字符串用户名,密码字符串,字符串姓氏,字符串姓)
{
ContentValues的args =新ContentValues();
args.put(KEY_USERNAME,用户名);
args.put(KEY_PASSWORD,密码);
args.put(KEY_LNAME,姓氏);
args.put(KEY_FNAME,名字);
返回db.update(DATABASE_TABLE,指定参数时,
KEY_ROWID +=+ ROWID,NULL)> 0;
}
私人布尔checkDataBase()
{
SQLiteDatabase CHECKDB = NULL;
尝试
{
字符串mypath中= DB_PATH + DB_NAME;
CHECKDB = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READWRITE);
}
赶上(SQLiteException E)
{
//数据库的简化版,存在。
}
如果(CHECKDB!= NULL)
{
checkDB.close();
}
返回CHECKDB!= NULL?真假;
}
公共无效的CreateDatabase()抛出IOException异常
{
布尔dbExist = checkDataBase();
如果(dbExist)
{
//什么也不做 - 数据库中已存在
}
其他
{
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE_2);
}
}
}
解决方案
您不包括 _id
在列列表,你在做查询 getSpinnerData()
。
the error in my logcat is such
Logcat
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): java.lang.RuntimeException: Unable to start activity ComponentInfo{one.two/one.two.Booking}: java.lang.IllegalArgumentException: column '_id' does not exist
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at android.os.Looper.loop(Looper.java:123)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at android.app.ActivityThread.main(ActivityThread.java:3948)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at java.lang.reflect.Method.invoke(Method.java:521)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at dalvik.system.NativeStart.main(Native Method)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at android.widget.CursorAdapter.init(CursorAdapter.java:111)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at android.widget.CursorAdapter.<init>(CursorAdapter.java:90)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:47)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:85)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at one.two.Booking.onCreate(Booking.java:34)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
Part of my DBAdapter.java
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
db = new DBAdapter(this);
db.open();
setContentView(R.layout.booking);
/*long id;
id = db.insertTime(
new String("08:00")); */
Cursor spinnerCursor = db.getSpinnerData();
startManagingCursor(spinnerCursor);
Spinner colourSpinner = (Spinner) findViewById(R.id.spinner);
Creation of DB
package one.two;
import java.io.IOException;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter
{
private static String DB_PATH = "/data/data/one.two/databases/";
private static String DB_NAME = "irsyad";
//values for the login table
public static final String KEY_ROWID = "_id";
public static final String KEY_USERNAME = "Username";
public static final String KEY_PASSWORD = "Password";
public static final String KEY_LNAME = "LastName";
public static final String KEY_FNAME ="FirstName";
public static final String KEY_ROWID2 = "_id";
public static final String KEY_STATUS = "status";
public static final String KEY_DESTINATION = "destination";
public static final String KEY_ARRIVAL = "arrival";
public static final String KEY_FERRY ="ferry";
private static final String TAG = "DBAdapter";
//declare Database name, tables names
private static final String DATABASE_NAME = "irsyad";
private static final String DATABASE_TABLE = "User";
private static final String DATABASE_TABLE_2 = "port";
private static final int DATABASE_VERSION = 1;
//declares the rules for the database tables
private static final String DATABASE_CREATE =
"create table user (_id integer primary key autoincrement, "
+ "Username text not null, Password text not null,"
+ "LastName text not null, FirstName text not null);";
private static final String DATABASE_CREATE_2 =
"create table port (_id integer primary key autoincrement, "
+ "status text null, destination text null,"
+ "arrival text null, ferry text null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
//Create the tables with the rules we set.
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE_2);
}
//OnUpgrade is only for use when u changed the database's version to 2 etc.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS port");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//Method for inserting login details, can be used in other java files when DBAdapter is
//declared in the java file. e.g. DBAdapter db = new DBAdapter(this);
public long insertUser(String Username, String Password, String LastName, String FirstName)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USERNAME, Username);
initialValues.put(KEY_PASSWORD, Password);
initialValues.put(KEY_LNAME, LastName);
initialValues.put(KEY_FNAME, FirstName);
return db.insert(DATABASE_TABLE, null, initialValues);
}
/* public long insertTime(String arrival)
{
ContentValues initialValue = new ContentValues();
initialValue.put(KEY_ARRIVAL, arrival);
return db.insert(DATABASE_TABLE_2, KEY_ARRIVAL, initialValue);
}*/
//---deletes a particular title---
public boolean deleteUser(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;
}
//method for retrieving all the inputs from database
public Cursor getAllUser()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_USERNAME,
KEY_PASSWORD,
KEY_LNAME,
KEY_FNAME,},
null,
null,
null,
null,
null);
}
public Cursor getAllData()
{
return db.query(DATABASE_TABLE_2, new String[] {
KEY_ROWID2,
KEY_STATUS,
KEY_DESTINATION,
KEY_ARRIVAL,
KEY_FERRY,},
null,
null,
null,
null,
null);
}
//---retrieves a particular title---
public Cursor getUser(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_USERNAME,
KEY_PASSWORD,
KEY_LNAME,
KEY_FNAME,},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getSpinnerData() throws SQLException
{
Cursor mCursor =
db.query(DATABASE_TABLE_2, new String[] {
KEY_ARRIVAL,},
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a title---
public boolean updateUser(long rowId, String Username, String Password, String LastName, String FirstName)
{
ContentValues args = new ContentValues();
args.put(KEY_USERNAME, Username);
args.put(KEY_PASSWORD, Password);
args.put(KEY_LNAME, LastName);
args.put(KEY_FNAME, FirstName);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
private boolean checkDataBase()
{
SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
catch(SQLiteException e)
{
//database does't exist yet.
}
if(checkDB != null)
{
checkDB.close();
}
return checkDB != null ? true : false;
}
public void createDataBase() throws IOException
{
boolean dbExist = checkDataBase();
if(dbExist)
{
//do nothing - database already exist
}
else
{
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE_2);
}
}
}
解决方案
You are not including _id
in your column list for the query you do in getSpinnerData()
.
这篇关于柱_id不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!