在SQLiteOpenHelper onCreate方法中显示ProgressDialog

时间:2014-01-06 17:14:18

标签: java android android-sqlite progressdialog

我是android编程的新手。我正在编写应该在第一次运行时执行sql文件的简单应用程序。但似乎这个过程需要几秒钟,所以我认为应用程序应该显示progressDialog,而它将执行sql文件。但是当我尝试运行应用程序时,对话框显示消息“app已停止...”。请帮帮我。

@Override
public void onCreate(SQLiteDatabase database)
{
    String CREATE_BIBLE_TABLE = "CREATE TABLE bible (" +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "book INTEGER, " +
            "chapter INTEGER NOT NULL, " +
            "verse INTEGER NOT NULL, " +
            "content TEXT" +
            ")";
    database.execSQL(CREATE_BIBLE_TABLE);

    new FirstLoadAsyncTask(database).execute();
}

public class FirstLoadAsyncTask extends AsyncTask<Void, Void, Void>
{
    private SQLiteDatabase database;
    private ProgressDialog progressDialog;

    public FirstLoadAsyncTask(SQLiteDatabase database)
    {
        this.database = database;
    }

    @Override
    protected void onPreExecute()
    {
        ((Activity) context).runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
                progressDialog = ProgressDialog.show(context, "Loading...", "");
            }
        });
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        try
        {
            InputStream inputStream = context.getAssets().open("bible.sql");
            execSqlFile(database, inputStream);
        } catch(IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        progressDialog.dismiss();
    }
} 

类扩展了SQLiteOpenHelper。

编辑: logcat的:

01-06 18:27:53.221  14118-14118/pl.several27.Biblia_Warszawska E/Trace﹕ error opening trace file: No such file or directory (2)
01-06 18:27:53.891  14118-14118/pl.several27.Biblia_Warszawska I/Adreno200-EGL﹕ <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.081_msm7627a_JB_REL_2.0.3_CL2820657_release_AU (CL2820657)
    Build Date: 01/22/13 Tue
    Local Branch:
    Remote Branch: quic/jb_rel_2.0.3
    Local Patches: NONE
    Reconstruct Branch: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.081 +  NOTHING
01-06 18:27:54.001  14118-14118/pl.several27.Biblia_Warszawska E/copybit﹕ Error opening frame buffer errno=13 (Permission denied)
01-06 18:27:54.001  14118-14118/pl.several27.Biblia_Warszawska W/Adreno200-EGLSUB﹕ <updater_create_surface_state:342>: updater_create_surface_state failed to open copybit, error: -13
01-06 18:27:54.011  14118-14118/pl.several27.Biblia_Warszawska D/memalloc﹕ ion: Mapped buffer base:0x53be8000 size:1536000 offset:0 fd:61
01-06 18:27:54.021  14118-14118/pl.several27.Biblia_Warszawska D/memalloc﹕ ion: Mapped buffer base:0x5083a000 size:4096 offset:0 fd:63
01-06 18:27:54.381  14118-14118/pl.several27.Biblia_Warszawska D/memalloc﹕ ion: Mapped buffer base:0x541fb000 size:1536000 offset:0 fd:66
01-06 18:27:54.381  14118-14118/pl.several27.Biblia_Warszawska D/memalloc﹕ ion: Mapped buffer base:0x50a50000 size:4096 offset:0 fd:68
01-06 18:27:54.501  14118-14118/pl.several27.Biblia_Warszawska D/memalloc﹕ ion: Mapped buffer base:0x54472000 size:1536000 offset:0 fd:70
01-06 18:27:54.501  14118-14118/pl.several27.Biblia_Warszawska D/memalloc﹕ ion: Mapped buffer base:0x50c75000 size:4096 offset:0 fd:72
01-06 18:27:55.001  14118-14118/pl.several27.Biblia_Warszawska D/memalloc﹕ ion: Mapped buffer base:0x545e9000 size:1536000 offset:0 fd:74
01-06 18:27:55.001  14118-14118/pl.several27.Biblia_Warszawska D/memalloc﹕ ion: Mapped buffer base:0x50d4c000 size:4096 offset:0 fd:76
01-06 18:27:57.231  14118-14118/pl.several27.Biblia_Warszawska D/book choosen﹕ 1
01-06 18:27:57.581  14118-14118/pl.several27.Biblia_Warszawska W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40ca4540)
01-06 18:27:57.601  14118-14118/pl.several27.Biblia_Warszawska E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.several27.Biblia_Warszawska/pl.several27.Biblia_Warszawska.ChapterActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
            at android.app.ActivityThread.access$600(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5520)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
            at pl.several27.Biblia_Warszawska.Database$FirstLoadAsyncTask.onPreExecute(Database.java:58)
            at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
            at android.os.AsyncTask.execute(AsyncTask.java:534)
            at pl.several27.Biblia_Warszawska.Database.onCreate(Database.java:42)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
            at pl.several27.Biblia_Warszawska.Database.countChapters(Database.java:148)
            at pl.several27.Biblia_Warszawska.ChapterActivity.onCreate(ChapterActivity.java:32)
            at android.app.Activity.performCreate(Activity.java:5066)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
            at android.app.ActivityThread.access$600(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5520)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
            at dalvik.system.NativeStart.main(Native Method)
01-06 18:27:59.511  14118-14118/pl.several27.Biblia_Warszawska D/Process﹕ killProcess, pid=14118
01-06 18:27:59.521  14118-14118/pl.several27.Biblia_Warszawska D/Process﹕ dalvik.system.VMStack.getThreadStackTrace(Native Method)
01-06 18:27:59.521  14118-14118/pl.several27.Biblia_Warszawska D/Process﹕ java.lang.Thread.getStackTrace(Thread.java:599)
01-06 18:27:59.521  14118-14118/pl.several27.Biblia_Warszawska D/Process﹕ android.os.Process.killProcess(Process.java:956)
01-06 18:27:59.521  14118-14118/pl.several27.Biblia_Warszawska D/Process﹕ com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108)
01-06 18:27:59.531  14118-14118/pl.several27.Biblia_Warszawska D/Process﹕ java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
01-06 18:27:59.531  14118-14118/pl.several27.Biblia_Warszawska D/Process﹕ java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
01-06 18:27:59.531  14118-14118/pl.several27.Biblia_Warszawska D/Process﹕ dalvik.system.NativeStart.main(Native Method)

我也试过这种方式display progressdialog in non-activity class,但它也不会起作用。

这是完整的应用程序源代码,但没有对话框:https://github.com/several27/BibliaWarszawska_Android

请有人帮助我吗?

2 个答案:

答案 0 :(得分:1)

我在我的应用程序上做了类似的事情,但我更喜欢在后台执行此操作,因此用户只是无法访问依赖于我的数据库的屏幕...

你可以尝试这样的事情:

public class BackgroundSyncService extends IntentService {

    public static final String NOTIFICATION = "com.example.sync.service";
public static final String RESULT = "result";

    public BackgroundSyncService() {
    super("BackgroundSyncService");
}

    @Override
protected void onHandleIntent(Intent intent) {
    //Do here what you want with your database

    //After all process you just notify your activitys
    Intent intent = new Intent(NOTIFICATION);
    intent.putExtra(RESULT, result);
sendBroadcast(intent);
    }

}

创建一个接收器(我在项目中使用内部类)

private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                    //Do what you want here , like enable a section of your app
            }
        }
    };

然后您需要将服务注册到您的活动中添加:

registerReceiver(receiver, new IntentFilter(BackgroundSyncService.NOTIFICATION));

不要忘记取消注册接收者:

@Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

另外,不要忘记在AndroidManifest.xml上注册您的IntentService

<service android:name="com.example.service.BackgroundSyncService" />

修改

此外,您需要在所需的位置包含调用以启动服务,我在App实例上启动:

Intent intent = new Intent(this, BackgroundSyncService.class);
startService(intent);

<强>说明

首先,您要创建一个服务来做您想做的事情,服务可以做任何您想做的事情,在您填写数据库的情况下......

创建此服务后,您将设置何时启动此服务(编辑部分)......

之后,您将注册您的活动以监听我们创建BroadcastReceiver的原因,当您的服务执行该行时将调用BroadcastReceiver:

 //After all process you just notify your activitys
    Intent intent = new Intent(NOTIFICATION);
    intent.putExtra(RESULT, result);
    sendBroadcast(intent);

答案 1 :(得分:0)

我想应用程序启动时更好的方式是向用户显示与数据库无关的消息/活动,或者只是在加载时使用启动画面并估计通常完成加载的时间为启动画面的计时器