2011-08-15 71 views
0

我試圖顯示一個進度對話框,當應用程序第一次運行,因爲它安裝數據庫,並需要一段時間,因爲數據庫的大小。這裏是我使用的ListAcitity: package samples.employeedirectory;如何在android中安裝sqllite數據庫時顯示進度對話框?

​​

}

隨後,我意識到在活動開始之前,我無法顯示進度條。實現這一目標的最佳方法是什麼?

編輯: 使用此代碼:

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) 

回答

0

可以使用Asyntask概念 在Preexecute()啓動進度對話 在doinginbackground()方法中安裝DB in postexecute()停止dialo GUE

參考this

+0

我編輯我的問題與我收到執行Asyntask錯誤。我在網上嘗試過可能的例子,但沒有一個能夠工作。 –

+0

請指定context = this;在您的活動類 –

+0

的onCreate()或dialog = ProgressDialog.show(EmployeeList.this,「」, 「Please wait for seconds seconds ...」,true); –

相關問題