我在使用 SQLite 数据库时遇到问题。当我尝试打开我的活动时,我收到强制关闭消息。它有一个微调器,用于显示数据库表中的数据。我可以打开活动,微调器显示表中的数据,遵循此示例 - Android:从 SQLite 数据库填充微调器 - SamColes。
这是我的代码和 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;
}
}
个人信息类
用于将数据库表中的数据显示到微调器中并将数据插入到喜欢表和不喜欢表中的代码。
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();
日志猫
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
我不想更改任何数据库适配器中的任何代码,因为我修改了
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()
BuddyDBAdapter 中导致了重大问题。另外,我已经尝试过了,它可以打开活动,但微调器中没有显示任何数据,也没有在数据库浏览器中创建任何表。