加载预加载数据库时android无法添加旋转轮对话框

时间:2013-07-21 17:23:54

标签: android database android-asynctask progressdialog

由于预加载数据库有点大,我想在第一次加载数据库时添加一个旋转轮进度对话框。编码如下:

MainActivity:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.exercises_main);    

    SharedPreferences settings = this.getSharedPreferences("MyApp",0);
    boolean firstrun=settings.getBoolean("firstrun_ex",true);           
    if (firstrun) 
    {
        SharedPreferences.Editor e = settings.edit();
        e.putBoolean("firstrun_ex",false);
        e.commit();
        new performBackgroundTask().execute();     //LINE 71
    }
            ...

private class performBackgroundTask  extends AsyncTask<Void, Void, Void> 
{
    @Override
    protected void onPreExecute()
    {           
        dialog= new ProgressDialog(Exercises_MainActivity.this);
        dialog.setCancelable(false);
        dialog.setMessage("Downloading! Please wait...!");
        dialog.show();                           
    }

    @Override
    protected Void doInBackground(Void... params) 
    {
        initDB();     //LINE 206
        return null;
    }

    protected void onPostExecute(Void result) 
    {
        super.onPostExecute(result);
        dialog.dismiss();
    }       
}

private void initDB()       //LINe217
{
    if(Ex_dbHlp == null)
        Ex_dbHlp = new Keepfit_Ex_DataBaseHelper(this);
        Ex_dbHlp.fillDB();  //LINE221
        exercises = Ex_dbHlp.get_All_Ex_Data();
}

如果没有AsyncTask,它可以正常工作,但加载initDB时屏幕将被锁定/黑屏:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.exercises_main);    

    SharedPreferences settings = this.getSharedPreferences("MyApp",0);
    boolean firstrun=settings.getBoolean("firstrun_ex",true);           
    if (firstrun) 
    {
        SharedPreferences.Editor e = settings.edit();
        e.putBoolean("firstrun_ex",false);
        e.commit();
        initDB();
    }

public void fillDB() 
{
    SQLiteDatabase db = getWritableDatabase();      
    ContentValues[] values = new ContentValues[63];
    for(int i=0; i<values.length; i++)
        values[i] = new ContentValues();

    values[0].put("id", "001");
    values[0].put("ex_group", "group1");
    values[0].put("ex_name", "name1");
    values[0].put("ex_cal", "cal1");
    for(ContentValues row : values)
    {
        db.insert(TABLE_NAME, null, row);
    }   
    db.close();
}

logcat的:

07-22 01:05:54.370: E/AndroidRuntime(28070): FATAL EXCEPTION: AsyncTask #1
07-22 01:05:54.370: E/AndroidRuntime(28070): java.lang.RuntimeException: An error occured while executing doInBackground()
07-22 01:05:54.370: E/AndroidRuntime(28070):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at java.lang.Thread.run(Thread.java:856)
07-22 01:05:54.370: E/AndroidRuntime(28070): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.abc.abc/databases/abc
07-22 01:05:54.370: E/AndroidRuntime(28070):    at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1437)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at com.abc.abc.Abc_Ex_DataBaseHelper.fillDB(Abc_Ex_DataBaseHelper.java:370)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at com.abc.abc.Exercises_MainActivity.initDB(Exercises_MainActivity.java:221)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at com.abc.abc.Exercises_MainActivity.access$0(Exercises_MainActivity.java:217)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at com.abc.abc.Exercises_MainActivity$performBackgroundTask.doInBackground(Exercises_MainActivity.java:206)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at com.abc.abc.Exercises_MainActivity$performBackgroundTask.doInBackground(Exercises_MainActivity.java:1)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-22 01:05:54.370: E/AndroidRuntime(28070):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-22 01:05:54.370: E/AndroidRuntime(28070):    ... 5 more
07-22 01:06:02.465: I/Choreographer(28070): Skipped 464 frames!  The application may be doing too much work on its main thread.
07-22 01:06:03.880: E/WindowManager(28070): Activity com.abc.abc.Exercises_MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4228c4c0 that was originally added here
07-22 01:06:03.880: E/WindowManager(28070): android.view.WindowLeaked: Activity com.abc.abc.Exercises_MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4228c4c0 that was originally added here
07-22 01:06:03.880: E/WindowManager(28070):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:403)
07-22 01:06:03.880: E/WindowManager(28070):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:311)
07-22 01:06:03.880: E/WindowManager(28070):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
07-22 01:06:03.880: E/WindowManager(28070):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
07-22 01:06:03.880: E/WindowManager(28070):     at android.view.Window$LocalWindowManager.addView(Window.java:554)
07-22 01:06:03.880: E/WindowManager(28070):     at android.app.Dialog.show(Dialog.java:277)
07-22 01:06:03.880: E/WindowManager(28070):     at com.abc.abc.Exercises_MainActivity$performBackgroundTask.onPreExecute(Exercises_MainActivity.java:200)
07-22 01:06:03.880: E/WindowManager(28070):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
07-22 01:06:03.880: E/WindowManager(28070):     at android.os.AsyncTask.execute(AsyncTask.java:534)
07-22 01:06:03.880: E/WindowManager(28070):     at com.abc.abc.Exercises_MainActivity.onCreate(Exercises_MainActivity.java:71)
07-22 01:06:03.880: E/WindowManager(28070):     at android.app.Activity.performCreate(Activity.java:5206)
07-22 01:06:03.880: E/WindowManager(28070):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-22 01:06:03.880: E/WindowManager(28070):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
07-22 01:06:03.880: E/WindowManager(28070):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
07-22 01:06:03.880: E/WindowManager(28070):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
07-22 01:06:03.880: E/WindowManager(28070):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
07-22 01:06:03.880: E/WindowManager(28070):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 01:06:03.880: E/WindowManager(28070):     at android.os.Looper.loop(Looper.java:137)
07-22 01:06:03.880: E/WindowManager(28070):     at android.app.ActivityThread.main(ActivityThread.java:4921)
07-22 01:06:03.880: E/WindowManager(28070):     at java.lang.reflect.Method.invokeNative(Native Method)
07-22 01:06:03.880: E/WindowManager(28070):     at java.lang.reflect.Method.invoke(Method.java:511)
07-22 01:06:03.880: E/WindowManager(28070):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
07-22 01:06:03.880: E/WindowManager(28070):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-22 01:06:03.880: E/WindowManager(28070):     at dalvik.system.NativeStart.main(Native Method)

问题:

我想在加载数据库时添加一个简单的旋转轮进度对话框...但它失败并报告上面的logcat。

如果我直接删除写initDB();在onCreate中,数据库可以正确加载,因此提取所有数据库数据应该没有问题。

如果那么上述问题怎么解决?非常感谢!!

0 个答案:

没有答案