簡體   English   中英

在 android 中安裝 sqllite 數據庫時如何顯示進度對話框?

[英]how to display progress dialog while installing sqllite database in 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)

您可以在 Preexecute() 中使用 Asyntask 概念 在doinginbackground() 方法中啟動進度對話 在 postexecute() 中安裝數據庫 停止對話

參考這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM