正确使用活动生命周期事件

时间:2013-11-08 07:12:16

标签: android android-lifecycle android-event

我有两项活动,即MainActivityFavorites。在MainActivity上,我有类似的内容:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  //function to insert data to database
  insertData();
}

我在MainActivity上有一个链接,指向Favorites活动。但是当点击“收藏夹”中的“向上导航按钮”返回到MainActivity时,应用程序正在关闭。

将数据插入onCreate事件是否理想?

请帮忙。谢谢。

这是日志:

11-08 15:50:14.848: E/SQLiteLog(8746): (5) database is locked
11-08 15:50:14.865: E/SQLiteDatabase(8746): Failed to open database '/data/data/example.pangasinantranslator/databases/dbTrans'.
11-08 15:50:14.865: E/SQLiteDatabase(8746): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:627)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:313)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:287)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:215)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:884)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at example.pangasinantranslator.MySQLiteHelper.addWord(MySQLiteHelper.java:46)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at example.pangasinantranslator.MainActivity.readFile(MainActivity.java:240)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at example.pangasinantranslator.MainActivity.onCreate(MainActivity.java:56)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.Activity.performCreate(Activity.java:5020)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.ActivityThread.access$600(ActivityThread.java:181)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.os.Looper.loop(Looper.java:153)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at android.app.ActivityThread.main(ActivityThread.java:5042)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at java.lang.reflect.Method.invokeNative(Native Method)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at java.lang.reflect.Method.invoke(Method.java:511)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
11-08 15:50:14.865: E/SQLiteDatabase(8746):     at dalvik.system.NativeStart.main(Native Method)
11-08 15:50:14.865: D/Here is error(8746): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
11-08 15:50:14.865: D/Insert:(8746): Inserting ..

1 个答案:

答案 0 :(得分:2)

在向用户显示屏幕之前,您可能需要做的任何事情都应该在onCreate()之前setContentView()完成。