我创建了一个数据库" dbase"在SQLiteManager中并保存在assets文件夹中。该DB包含1个表"注释" with id(primary ket,integer,autoincrement)ane comment(text)fields.Logcat显示错误:
我的活动课程是:
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class MyActivity extends Activity {
private Cursor em;
private MyDatabase db;
TextView t;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
try {
db = new MyDatabase(getApplicationContext());
em = db.getEmployees();
t = (TextView) findViewById(R.id.txt);
if (em != null) {
Integer com = em.getCount();
t.setText(com);
do {
String comment = em.getString(1);
t.setText(comment);
} while (em.moveToNext());
}
}
catch(SQLiteAssetHelper.SQLiteAssetException exception)
{ Log.d("checkDatabase", exception.toString());}
}
@Override
protected void onDestroy() {
super.onDestroy();
em.close();
db.close();
}
我的数据库类是:
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class MyDatabase extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "dbase.db";
private static final int DATABASE_VERSION = 1;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public Cursor getEmployees() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqlSelect = {"comment"};
String sqlTables = "comments";
qb.setTables(sqlTables);
Cursor c= db.query(sqlTables,sqlSelect,null,null,null,null,null);
c.moveToFirst();
return c;
}
}
logcat打印:
09-08 14:44:03.789 14933-14933/com.example.iyas.ex2 I/SQLiteAssetHelper﹕ successfully opened database dbase.db
09-08 14:44:03.934 14933-14933/com.example.iyas.ex2 W/ResourceType﹕ No package identifier when getting value for resource number 0x00000004
09-08 14:44:04.033 14933-14933/com.example.iyas.ex2 D/AndroidRuntime﹕ Shutting down VM
09-08 14:44:04.078 14933-14933/com.example.iyas.ex2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.iyas.ex2, PID: 14933
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iyas.ex2/com.example.iyas.ex2.MyActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x4
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.access$800(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5070)
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:836)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x4
我的xml文件:
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
的TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/txt"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
/ RelativeLayout的>