首先,我是我正在学习的 android 的新开发人员,所以请多多包涵。我正在制作一个与书籍章节相关的应用程序并显示其内容,我从数据库中填充了工作列表视图,但面临一些错误:-
在离开应用程序时,我强制关闭查看日志猫。
在列表视图中,我正在将新数据附加到数据库中。我希望它只有一次。因为章节名称对于一本书来说是不变的
单击章节时,应打开选定的章节列表。
请让我知道如何纠正它们
主要活动
public class MainActivity extends Activity {
DBADChapter myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openDB();
registerListClickCallback();
}
@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
myDb.deleteAll();
}
private void openDB() {
myDb = new DBADChapter(this);
myDb.open();
myDb.insertRow(999, "TITLE 1");
myDb.insertRow(991, "TITLE 2");
myDb.insertRow(992, "TITLE 3");
myDb.insertRow(993, "TITLE 4");
populatelistviewfromDB();
}
private void closeDB() {
myDb.close();
myDb.deleteAll();
}
public void onClick_AddRecord(View v) {
}
private void populatelistviewfromDB() {
Cursor cursor = myDb.getAllRows();
startManagingCursor(cursor);
String[] fromFieldNames = new String[] { DBADChapter.KEY_CHEPTERNUM,
DBADChapter.KEY_CHAPTERHEADING };
int[] toViewIds = new int[] { R.id.tvchapterno, R.id.tvchapter };
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,
R.layout.item_view, cursor, fromFieldNames, toViewIds);
ListView myList = (ListView) findViewById(R.id.listchapter);
myList.setAdapter(myCursorAdapter);
}
private void registerListClickCallback() {
ListView myList = (ListView) findViewById(R.id.listchapter);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long idInDB) {
Cursor cursor = myDb.getRow(idInDB);
if (cursor.moveToFirst()) {
}
cursor.close();
}
});
}
@Override
protected void onPause() {
super.onPause();
closeDB();
myDb.deleteAll();
}
}
数据库
public class DBADChapter2 {
private static final String TAG = "DBAdapter";
public static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;
public static final String KEY_CHEPTERNUM = "chapternum";
public static final String KEY_CHAPTERHEADING = "chapterheading";
public static final int COL_CHEPTERNUM = 1;
public static final int COL_CHAPTERHEADING = 2;
public static final String[] ALL_KEYS = new String[] { KEY_ROWID,
KEY_CHEPTERNUM, KEY_CHAPTERHEADING };
public static final String DATABASE_NAME = "MyDb";
public static final String DATABASE_TABLE = "chapters";
public static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE_SQL = "create table "
+ DATABASE_TABLE + " (" + KEY_ROWID
+ " integer primary key autoincrement, "
+ KEY_CHEPTERNUM + " integer not null, " + KEY_CHAPTERHEADING
+ " string not null"
+ ");";
private final Context context;
private DatabaseHelperChapter myDBHelper;
private SQLiteDatabase db;
public DBADChapter2(Context ctx) {
this.context = ctx;
myDBHelper = new DatabaseHelperChapter(context);
}
public DBADChapter2 open() {
db = myDBHelper.getWritableDatabase();
return this;
}
public void close() {
myDBHelper.close();
}
public long insertRow(int number, String chapterheadings) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_CHEPTERNUM, number);
initialValues.put(KEY_CHAPTERHEADING, chapterheadings);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return db.delete(DATABASE_TABLE, where, null) != 0;
}
public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null,
null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null,
null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
private static class DatabaseHelperChapter extends SQLiteOpenHelper {
DatabaseHelperChapter(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version "
+ oldVersion + " to " + newVersion
+ ", which will destroy all old data!");
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(_db);
}
}
}
日志猫
10-06 18:52:30.822: E/AndroidRuntime(564): FATAL EXCEPTION: main
10-06 18:52:30.822: E/AndroidRuntime(564): java.lang.RuntimeException: Unable to pause activity {com.bookz.bookz1/com.bookz.bookz1.MainActivity}: java.lang.IllegalStateException: database not open
10-06 18:52:30.822: E/AndroidRuntime(564): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2354)
10-06 18:52:30.822: E/AndroidRuntime(564): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2311)
10-06 18:52:30.822: E/AndroidRuntime(564): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2291)
10-06 18:52:30.822: E/AndroidRuntime(564): at android.app.ActivityThread.access$1700(ActivityThread.java:117)
10-06 18:52:30.822: E/AndroidRuntime(564): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
10-06 18:52:30.822: E/AndroidRuntime(564): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 18:52:30.822: E/AndroidRuntime(564): at android.os.Looper.loop(Looper.java:123)
10-06 18:52:30.822: E/AndroidRuntime(564): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-06 18:52:30.822: E/AndroidRuntime(564): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 18:52:30.822: E/AndroidRuntime(564): at java.lang.reflect.Method.invoke(Method.java:507)
10-06 18:52:30.822: E/AndroidRuntime(564): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-06 18:52:30.822: E/AndroidRuntime(564): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-06 18:52:30.822: E/AndroidRuntime(564): at dalvik.system.NativeStart.main(Native Method)
10-06 18:52:30.822: E/AndroidRuntime(564): Caused by: java.lang.IllegalStateException: database not open
10-06 18:52:30.822: E/AndroidRuntime(564): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1230)
10-06 18:52:30.822: E/AndroidRuntime(564): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1189)
10-06 18:52:30.822: E/AndroidRuntime(564): at com.bookz.bookz1.DBADChapter.getAllRows(DBADChapter.java:130)
10-06 18:52:30.822: E/AndroidRuntime(564): at com.bookz.bookz1.DBADChapter.deleteAll(DBADChapter.java:117)
10-06 18:52:30.822: E/AndroidRuntime(564): at com.bookz.bookz1.MainActivity.closeDB(MainActivity.java:53)
10-06 18:52:30.822: E/AndroidRuntime(564): at com.bookz.bookz1.MainActivity.onPause(MainActivity.java:79)
10-06 18:52:30.822: E/AndroidRuntime(564): at android.app.Activity.performPause(Activity.java:3851)
10-06 18:52:30.822: E/AndroidRuntime(564): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
10-06 18:52:30.822: E/AndroidRuntime(564): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2341)
10-06 18:52:30.822: E/AndroidRuntime(564): ... 12 more