1

我有一个 SQLiteHelper 源文件,我用它来创建数据库等等。我有另一个需要实例化数据库、提取数据并显示到 UI 的源文件。但是,当我在模拟器上启动应用程序时,应用程序崩溃了。我不确定出了什么问题。源文件中没有给出错误。

餐厅数据库源文件

package com.demostudio.restaurants;

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

public class RestaurantDB extends SQLiteOpenHelper {

static final String dbName = "RestaurantDB";
static final Integer dbVersion = 1;

static final String restaurantListTable = "RestaurantList";
static final String colRestaurantID = "RestaurantID";
static final String colRestaurantName = "RestaurantName";
static final String colRestaurantStore = "StoreNo";

public RestaurantDB(Context context) {
super(context, dbName, null, dbVersion);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

String newTableSQL = 
        "CREATE TABLE "+
        restaurantListTable+
        " ("+colRestaurantID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
        colRestaurantName+" TEXT NOT NULL, "+
        colRestaurantStore+" TEXT NOT NULL "+
        ");";
db.execSQL(newTableSQL);

ContentValues cv = new ContentValues();
cv.put(colRestaurantName, "Restaurant AAA");
cv.put(colRestaurantStore, "1");
db.insert(restaurantListTable, null, cv);

cv.put(colRestaurantName, "Restaurant BBB");
cv.put(colRestaurantStore, "2");
db.insert(restaurantListTable, null, cv);

db.close();

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}
}

AllRestaurants 源文件

package com.demostudio.restaurants;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;

public class AllRestaurants extends ListActivity {
 RestaurantDB db = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);  
 db=new RestaurantDB(AllRestaurants.this);    
onPostExecute( ) ;
}

private Cursor doQuery() {
return(db.getReadableDatabase().rawQuery("SELECT colRestaurantID AS _id, colRestaurantName, colRestaurantStore "
+ "FROM RestaurantListTable ORDER BY colRestaurantName", null));

}

public void onPostExecute( ) {
SimpleCursorAdapter adapter;

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
    adapter=new SimpleCursorAdapter(this, R.layout.activity_all_restaurants,
            doQuery(), new String[] {
        RestaurantDB.colRestaurantName,
        RestaurantDB.colRestaurantStore },
        new int[] { R.id.title, R.id.value },
        0);
    }
else {
    adapter=new SimpleCursorAdapter(this, R.layout.activity_all_restaurants,
            doQuery(), new String[] {
        RestaurantDB.colRestaurantName,
        RestaurantDB.colRestaurantStore },
        new int[] { R.id.title, R.id.value });
    }


setListAdapter(adapter);
}
}

活动列表视图 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
    android:id="@android:id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

activity_all_restaurants XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="20sp"
android:textStyle="bold"/>

<TextView
android:id="@+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="20sp"
android:textStyle="bold"/>

</RelativeLayout>

日志文件

12-13 14:08:52.805:W/Trace(3861):来自 nativeGetEnabledTags 的意外值:0
12-13 14:08:52.805:W/Trace(3861):来自 nativeGetEnabledTags 的意外值:0
12-13 14:08 :54.795: D/dalvikvm(3861): GC_FOR_ALLOC 释放 68K, 7% 释放 2670K/2860K, 暂停 139ms, 总共 143ms
12-13 14:08:54.826: I/dalvikvm-heap(3861): Grow heap (frag case)为 3.796MB 分配 1127536 字节
12-13 14:08:54.946: D/dalvikvm(3861): GC_FOR_ALLOC 释放 1K, 5% 空闲 3770K/3964K, 暂停 112ms, 总共 112ms
12-13 14:08:55.145: D /dalvikvm(3861): GC_CONCURRENT freed <1K, 5% free 3785K/3964K, paused 12ms+27ms, total 203ms
12-13 14:08:55.618: D/AndroidRuntime(3861): 关闭VM
12-13 14:08:55.618: W/dalvikvm(3861): threadid=1: 线程退出未捕获异常 (group=0x40a70930)
12-13 14:08:55.715: E/AndroidRuntime(3861): 致命异常: main
12-13 14:08:55.715: E/AndroidRuntime(3861): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.demostudio.restaurants/com.demostudio.restaurants.AllRestaurants}: java.lang.IllegalArgumentException: column '_id' 不存在
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-13 14:08:55.715: E/AndroidRuntime( 3861): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-13 14:08:55.715: E/AndroidRuntime(3861): 在 android.app.ActivityThread.access$600(ActivityThread.java:141)
12-13 14:08:55.715: E/AndroidRuntime(3861): 在 android .app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-13 14:08:55.715: E/AndroidRuntime(3861): 在 android.os.Handler.dispatchMessage(Handler.java:99)
12-13 14 :08:55.715: E/AndroidRuntime(3861): 在 android.os.Looper.loop(Looper.java:137)
12-13 14:08:55.715: E/AndroidRuntime(3861): 在 android.app.ActivityThread。 main(ActivityThread.java:5039)
12-13 14:08:55.715: E/AndroidRuntime(3861): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 14:08:55.715: E/AndroidRuntime (3861): 在 java.lang.reflect.Method.invoke(Method.java:511)
12-13 14:08:55.715: E/AndroidRuntime(3861): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-13 14:08:55.715: E/AndroidRuntime( 3861): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-13 14:08:55.715: E/AndroidRuntime(3861): 在 dalvik.system.NativeStart.main(Native Method)
12-13 14:08:55.715: E/AndroidRuntime(3861): 由: java.lang.IllegalArgumentException: 列 '_id' 不存在
12-13 14:08:55.715: E/AndroidRuntime(3861): 在 android .database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
12-13 14:08:55.715: E/AndroidRuntime(3861): 在 android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
12-13 14:08:55.715: E/AndroidRuntime(3861): 在 android.support.v4.widget.CursorAdapter.(CursorAdapter.java:151)
12-13 14:08:55.715: E/AndroidRuntime(3861):在 android.support.v4.widget.ResourceCursorAdapter.(ResourceCursorAdapter.java:93)
12-13 14:08:55.715: E/AndroidRuntime(3861): 在 android.support.v4.widget.SimpleCursorAdapter.(SimpleCursorAdapter.java: 89)
12-13 14:08:55.715: E/AndroidRuntime(3861): 在 com.demostudio.restaurants.AllRestaurants.onPostExecute(AllRestaurants.java:27)
12-13 14:08:55.715: E/AndroidRuntime(3861) : 在 com.demostudio.restaurants.AllRestaurants.onCreate(AllRestaurants.java:18)
12-13 14:08:55.715: E/AndroidRuntime(3861): 在 android.app.Activity.performCreate(Activity.java:5104)
12-13 14:08:55.715: E/AndroidRuntime(3861): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-13 14:08:55.715: E/AndroidRuntime(3861): 在 android. app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-13 14:08:55.715: E/AndroidRuntime(3861): ... 11 更多

我之前将 listview 与两个 textviews 放在同一个 XML 文件中,但它不起作用。所以现在我把它放在一个单独的 XML 文件中,但它仍然不起作用。

4

3 回答 3

2

在你onCreate()的助手中,你有:

String newTableSQL = 
    "CREATE TABLE "+
    `+
    " ("+colRestaurantID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
    colRestaurantName+" TEXT NOT NULL, "+
    colRestaurantStore+" TEXT NOT NULL "+
    ");";

请注意您如何连接字符串以创建完整的语句。生成的 SQL 将是:

CREATE TABLE RestaurantList (RestaurantID INTEGER PRIMARY KEY AUTOINCREMENT, RestaurantName TEXT NOT NULL, RestaurantStore TEXT NOT NULL );

根据您对restaurantListTable, colRestaurantID, colRestaurantName,的值colRestaurantStore

但是,在您的rawQuery()通话中,您的 SQL 是这样的:

SELECT colRestaurantID AS _id, colRestaurantName, colRestaurantStore FROM RestaurantListTable ORDER BY  colRestaurantName

rawQuery()请注意,您在SQL中的列名在col它们前面,而您的CREATE TABLE. 这不起作用,因为您需要列名保持一致。

是否更改您的CREATE TABLE陈述或SELECT陈述取决于您。

于 2012-12-13T15:18:57.343 回答
0
private Cursor doQuery() {
return(db.getReadableDatabase().rawQuery("SELECT colRestaurantID AS _id,    colRestaurantName, colRestaurantStore "
+ "FROM RestaurantListTable ORDER BY colRestaurantName", null));
}

我应该改用以下内容:

private Cursor doQuery() {
return(db.getReadableDatabase().rawQuery("SELECT RestaurantID AS _id, RestaurantName, StoreNo "
+ "FROM RestaurantList ORDER BY RestaurantName", null));
}
于 2012-12-13T15:18:51.323 回答
0

你又做了。如果您需要数据库实例化一个字段,请更改RestaurantDB db=new RestaurantDB(AllRestaurants.this);db=new RestaurantDB(AllRestaurants.this);

于 2012-12-12T15:43:14.833 回答