我遇到了SQLite數據庫問題。當我嘗試打開我的活動時,我會收到強制關閉消息。它有一個微調器,用於顯示數據庫表中的數據。我可以打開該活動,並且微調器顯示錶中的數據,以下示例 - Android: populate a Spinner from a SQLite database - SamColes。嘗試打開活動並在Spinner中顯示數據庫時強制關閉
這是我的代碼和LogCat。
AnniversaryAdapter - 5個表創建
public class AnniversaryDBAdapter
{
private static final String DATABASE_NAME = "Tables";
private static final int DATABASE_VERSION = 2;
private static final String CREATE_TABLE_TITLE = "create table titles(title_id integer primary key autoincrement, title text not null, image text not null);";
private static final String CREATE_TABLE_BUDDIESLIST = "create table buddiesList(name_id integer primary key autoincrement, name text not null);";
private static final String CREATE_TABLE_LIKES = "create table likes(likes_id integer primary key autoincrement,like text not null, name_id integer not null);";
private static final String CREATE_TABLE_DISLIKES = "create table dislikes(dlike_id integer primary key autoincrement, dislike text not null, name_id integer not null);";
private static final String CREATE_TABLE_EVENTS = "create table event(event_id integer primary key autoincrement, title_id integer not null, location text not null, starttime text not null, endtime text not null, desc text not null, date text not null, name_id integer not null);";
private final Context context;
private static final String TAG = "DBAdapter";
private DatabaseHelper DBHelper;
protected SQLiteDatabase db;
private static String DB_PATH = "/data/data/main.page/Tables/";
public AnniversaryDBAdapter(Context aContext)
{
this.context = aContext;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
/*try
{
database.execSQL(CREATE_TABLE_BUDDIESLIST);
database.execSQL(CREATE_TABLE_LIKES);
database.execSQL(CREATE_TABLE_EVENTS);
database.execSQL(CREATE_TABLE_TITLE);
database.execSQL(CREATE_TABLE_DISLIKES);
}catch(SQLException e)
{
e.printStackTrace();
}*/
db.execSQL(CREATE_TABLE_BUDDIESLIST);
db.execSQL(CREATE_TABLE_LIKES);
db.execSQL(CREATE_TABLE_EVENTS);
db.execSQL(CREATE_TABLE_TITLE);
db.execSQL(CREATE_TABLE_DISLIKES);
/* database.execSQL("CREATE TRIGGER fk_budevent_nameid" +
"BEFORE INSERT" +
"ON events FOR EACH ROW BEGIN" +
"SELECT CASE WHEN((SELECT name_id FROM buddiesList WHERE name_id = new.name_id) IS NULL)" +
"THEN RAISE(ABORT, 'Foreign Key Violation')END;" +
"END;");
*/
}
@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");
onCreate(db);
}
}
public AnniversaryDBAdapter open() throws SQLException
{
db = this.DBHelper.getWritableDatabase();
/* if(!db.isReadOnly())
{
db.execSQL("PRAGMA foreign_keys = ON;");
}*/
return this;
}
public void close()
{
DBHelper.close();
}
/*
public long insertEvent(String title,String location,String starttime,String endtime,String desc,String date, String name)
{
ContentValues cValues = new ContentValues();
cValues.put(KEY_TITLE, title);
cValues.put(KEY_LOCATION, location);
cValues.put(KEY_START, starttime);
cValues.put(KEY_END, endtime);
cValues.put(KEY_DESC, desc);
cValues.put(KEY_ALARM, alarm);
cValues.put(KEY_DATE, date);
cValues.put(KEY_NAME, name);
return db.insert(CREATE_TABLE_EVENTS, null, cValues);
}
public boolean deleteEvent(long rowId)
{
return db.delete(CREATE_TABLE_EVENTS, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor getAllEvents()
{
return db.query(CREATE_TABLE_EVENTS, new String[] {KEY_ROWID, KEY_TITLE, KEY_LOCATION, KEY_START, KEY_END, KEY_DESC, KEY_DATE, KEY_NAME}, null, null, null, null, null);
}
public Cursor getEvent(long rowId) throws SQLException
{
Cursor c = db.query(true,CREATE_TABLE_EVENTS, new String[] {KEY_ROWID, KEY_TITLE, KEY_LOCATION, KEY_START, KEY_END, KEY_DESC, KEY_DATE, KEY_NAME}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if(c != null)
{
c.moveToFirst();
}
return c;
}
*/
}
BuddyDBAdapter - buddiesList表
public class BuddyDBAdapter extends AnniversaryDBAdapter
{
public static final String KEY_ROWID = "name_id";
public static final String KEY_NAME = "name";
private static final String TAG = "DBAdapter";
private static final String CREATE_TABLE_BUDDIESLIST = "buddiesList";
//private SQLiteDatabase db;
public BuddyDBAdapter(Context aContext)
{
super(aContext);
}
public long insertNames(String name)
{
ContentValues buddyValues = new ContentValues();
buddyValues.put(KEY_NAME, name);
return db.insert(CREATE_TABLE_BUDDIESLIST, null, buddyValues);
}
public boolean deleteNames(long rowId)
{
return db.delete(CREATE_TABLE_BUDDIESLIST, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor getAllNames()
{
return db.query(CREATE_TABLE_BUDDIESLIST, new String[] { KEY_ROWID, KEY_NAME }, null, null, null, null, null);
}
public Cursor getNames(long rowId) throws SQLException
{
Cursor c = db.query(true, CREATE_TABLE_BUDDIESLIST, new String[] { KEY_ROWID, KEY_NAME }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if(c != null)
{
c.moveToFirst();
}
return c;
}
}
PersonalInformation.class
代碼,用於顯示從數據庫表中的數據到所說的紡絲器和數據插入喜歡桌子和不喜歡桌子。
BuddyDBAdapter buddyDB = new BuddyDBAdapter(this);
buddyDB.open();
sendTo = (Spinner) findViewById(R.id.sendTo);
Cursor friendsCursor = buddyDB.getAllNames();
startManagingCursor(friendsCursor);
String[] from = new String[]{BuddyDBAdapter.KEY_NAME};
int[] to = new int[]{R.id.to};
SimpleCursorAdapter friendsAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, friendsCursor, from, to);
friendsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sendTo.setAdapter(friendsAdapter);
sendTo.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
Cursor c = (Cursor)parent.getItemAtPosition(pos);
namesSpinnderId = c.getInt(c.getColumnIndexOrThrow(BuddyDBAdapter.KEY_ROWID));
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
buddyDB.close();
logcat的
09-26 15:43:42.188: I/dalvikvm(658): threadid=3: reacting to signal 3
09-26 15:43:42.198: I/dalvikvm(658): Wrote stack traces to '/data/anr/traces.txt'
09-26 15:43:42.578: D/gralloc_goldfish(658): Emulator without GPU emulation detected.
09-26 15:43:45.288: D/AndroidRuntime(658): Shutting down VM
09-26 15:43:45.288: W/dalvikvm(658): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
09-26 15:43:45.308: E/AndroidRuntime(658): FATAL EXCEPTION: main
09-26 15:43:45.308: E/AndroidRuntime(658): java.lang.RuntimeException: Unable to start activity ComponentInfo{main.page/main.page.PersonalInformation}: java.lang.NullPointerException
09-26 15:43:45.308: E/AndroidRuntime(658): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-26 15:43:45.308: E/AndroidRuntime(658): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-26 15:43:45.308: E/AndroidRuntime(658): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-26 15:43:45.308: E/AndroidRuntime(658): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-26 15:43:45.308: E/AndroidRuntime(658): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 15:43:45.308: E/AndroidRuntime(658): at android.os.Looper.loop(Looper.java:137)
09-26 15:43:45.308: E/AndroidRuntime(658): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-26 15:43:45.308: E/AndroidRuntime(658): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 15:43:45.308: E/AndroidRuntime(658): at java.lang.reflect.Method.invoke(Method.java:511)
09-26 15:43:45.308: E/AndroidRuntime(658): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-26 15:43:45.308: E/AndroidRuntime(658): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-26 15:43:45.308: E/AndroidRuntime(658): at dalvik.system.NativeStart.main(Native Method)
09-26 15:43:45.308: E/AndroidRuntime(658): Caused by: java.lang.NullPointerException
09-26 15:43:45.308: E/AndroidRuntime(658): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
09-26 15:43:45.308: E/AndroidRuntime(658): at main.page.AnniversaryDBAdapter.open(AnniversaryDBAdapter.java:96)
09-26 15:43:45.308: E/AndroidRuntime(658): at main.page.PersonalInformation.onCreate(PersonalInformation.java:55)
09-26 15:43:45.308: E/AndroidRuntime(658): at android.app.Activity.performCreate(Activity.java:4465)
09-26 15:43:45.308: E/AndroidRuntime(658): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-26 15:43:45.308: E/AndroidRuntime(658): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-26 15:43:45.308: E/AndroidRuntime(658): ... 11 more
我不想改變任何數據庫適配器中的任何代碼,因爲我在BuddyDBAdapter修改
return db.delete(CREATE_TABLE_BUDDIESLIST, KEY_ROWID + "=" + rowId, null) > 0;
到
if(db != null)
return db.query(CREATE_TABLE_BUDDIESLIST, new String[] { KEY_ROWID, KEY_NAME }, null, null, null, null, null);
return null;
在getAllNames()
w ^這造成了重大問題。另外,我已經嘗試過了,它可以打開活動,但沒有數據顯示在微調器中,並且沒有數據表在數據庫瀏覽器中創建。
您必須添加「_id」列才能使用CursorAdapter,對於Logcut更新,DBHelper未初始化,我認爲您已經向它傳遞了空上下文。請確保您在更改數據庫時刪除所有數據庫。 –
爲什麼你要在這個階段關閉數據庫? buddyDB.close(); 必須從Destroy調用,然後再殺死你的活動 –
我認爲它通常應該打開和關閉數據庫。我不想殺我的活動,也沒有我的任何類中的onDestroy方法。加我想我應該初始化DBHelper像這樣'私人DatabaseHelper DBHelper;'&'DBHelper = new DatabaseHelper(上下文);'在AnnivesaryDBAdapter我已經這麼做了嗎? – Preeyah