如何在android中安装sqlite数据库时显示进度对话框?

时间:2011-08-15 10:50:19

标签: java android

我试图在应用程序第一次运行时显示进度对话框,因为它安装了数据库,并且由于数据库大小需要一段时间才能完成。这是我正在使用的ListAcitity:     package samples.employeedirectory;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ListAdapter;
import android.widget.ListView;

public class EmployeeList extends ListActivity {

protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new progressBar().execute();
    db = (new DatabaseHelper(this)).getWritableDatabase();
    cursor = db.rawQuery("SELECT * FROM employeeList", new String[]{});

    adapter = new qCursorAdapter(this, cursor);

    // requestWindowFeature(Window.FEATURE_NO_TITLE);
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setListAdapter(adapter);

}

public void onListItemClick(ListView parent, View view, int position, long id) {
    Intent intent = new Intent(this, displayEmployee.class);
    Cursor cursor = (Cursor) adapter.getItem(position);
    intent.putExtra("eID", cursor.getInt(cursor.getColumnIndex("eID")));
    startActivity(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.pref:
            // Launch Preference activity
            Intent intent = new Intent(this, Preferences.class);
            startActivity(intent);
         break;
    }
    return true;
}

}

我意识到在活动开始之前我无法显示进度条。实现这一目标的最佳方法是什么?

编辑: 使用此代码:

package samples.employeedirectory;

import android.app.ProgressDialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

public class progressBar extends AsyncTask<String, Void, String> {
protected Context context;
protected SQLiteDatabase db;
protected ProgressDialog dialog;
@Override
protected String doInBackground(String... params) {
    db = (new DatabaseHelper(context)).getWritableDatabase();
    return null;
}

/* (non-Javadoc)
 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
 */
@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    dialog.dismiss();
}

/* (non-Javadoc)
 * @see android.os.AsyncTask#onPreExecute()
 */
@Override
protected void onPreExecute() {
    // super.onPreExecute();
dialog = ProgressDialog.show(context, "",
            "Please wait for few seconds...", true);
}

/* (non-Javadoc)
 * @see android.os.AsyncTask#onProgressUpdate(Progress[])
 */
@Override
protected void onProgressUpdate(Void... values) {
    // TODO Auto-generated method stub
    super.onProgressUpdate(values);
}

}

我收到了这些错误:

    08-15 23:21:26.743: ERROR/AndroidRuntime(5170): java.lang.RuntimeException: Unable to start activity ComponentInfo{samples.employeedirectory/samples.employeedirectory.EmployeeList}: java.lang.NullPointerException
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.app.ActivityThread.access$1500(ActivityThread.java:121)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.os.Handler.dispatchMessage(Handler.java:99)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.os.Looper.loop(Looper.java:123)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.app.ActivityThread.main(ActivityThread.java:3701)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at java.lang.reflect.Method.invokeNative(Native Method)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at java.lang.reflect.Method.invoke(Method.java:507)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at dalvik.system.NativeStart.main(Native Method)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170): Caused by: java.lang.NullPointerException
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.app.Dialog.<init>(Dialog.java:141)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.app.AlertDialog.<init>(AlertDialog.java:67)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.app.ProgressDialog.<init>(ProgressDialog.java:80)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.app.ProgressDialog.<init>(ProgressDialog.java:76)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.app.ProgressDialog.show(ProgressDialog.java:101)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.app.ProgressDialog.show(ProgressDialog.java:90)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at samples.employeedirectory.progressBar.onPreExecute(progressBar.java:34)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.os.AsyncTask.execute(AsyncTask.java:391)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at samples.employeedirectory.EmployeeList.onCreate(EmployeeList.java:27)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    08-15 23:21:26.743: ERROR/AndroidRuntime(5170):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)

1 个答案:

答案 0 :(得分:0)

你可以使用Asyntask概念 在Preexecute()中启动进度对话 在doneinbackground()方法中安装DB 在postexecute()中停止对话

参考this