Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4324486
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:08:20+00:00 2026-05-21T09:08:20+00:00

I need to do the following: when app is started it runs an activity

  • 0

I need to do the following: when app is started it runs an activity (splashActivity) which tries to create an DBHelper (SQLiteDatabase) instance which at creation time checks if database is exists and creates one if it doesn’t.

In this DB creation process i want to show the ProgressDialog and prevent the main thread to process while DB is not ready.

the splashActivity:

public class SplashActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        showSplash();
        DBHelper dbHelper = new DBHelper(this);
        dbHelper.close();
  }
}

the DBHelper class:

public class DBHelper extends SQLiteOpenHelper { 
...
    private ProgressDialog mProgressDialog;
...
  public DBHelper(Context context) { 
    super(context, DB_NAME, null, 1);
    appContext=context;
    dbReadable = getReadableDatabase();
    if (createDB)
        initDB();
  } 
  @Override 
  public void onCreate(SQLiteDatabase db) {
      createDB=true;
  } 
  public void initDB() {
      ...
      if (createDB) 
          restoreDB();
      ...
  }

  private void restoreDB(){
   // place for db creation/restoring method call (restoreDBFromFile() etc)
   // and displaying the progress
  }

I tried to make AsyncTask as DBHelper’s inner class:

  private class RestoreDBTask extends AsyncTask <Void, Void, String>
    {
        private ProgressDialog dialog;

        @Override
        protected void onPreExecute()
        {
            this.dialog = ProgressDialog.show(DBHelper.this.appContext, DBHelper.this.appContext.getResources().getString(R.string.progress_wait), 
                    DBHelper.this.appContext.getResources().getString(R.string.progress_db_installing), true);
        }

        @Override
        protected String doInBackground(Void... params)
        {
            restoreDBFromFile();
            return "";
        }

        @Override
        protected void onPostExecute(String result)
        {
            this.dialog.dismiss();
        }
    }

so the restoreDB() was:

restoreDB() {
      RestoreDBTask task = new RestoreDBTask();
      task.execute();
}

it starts task showing PD but meanwhile the main thread continues and so it crashes cause DB wasn’t ready. That means main thread should be suspended while DB is not ready so i tried to sleep/wait/join with no success. Ok. it’s not good anyway.

Next time i made call to restoreDBFromFile() from restoreDB() so that async task was used only for PD displaying:

restoreDB() {
  RestoreDBTask task = new RestoreDBTask();
  task.execute();
  restoreDBFromFile();    
}

It doesn’t display any PD. So I tried to create PD right in the restoreDB():

restoreDB() {
   mProgressDialog = ProgressDialog.show(appContext, 
          appContext.getResources().getString(R.string.progress_wait), 
          appContext.getResources().getString(R.string.progress_db_installing), 
          true);
  restoreDBFromFile();    
}

but no PD either! Next time i tried the following:

    restoreDB() {
      Thread thread =  new Thread(null, new Runnable(){
              @Override
              public void run() {
                  mProgressDialog = ProgressDialog.show(appContext, 
                          appContext.getResources().getString(R.string.progress_wait), 
                          appContext.getResources().getString(R.string.progress_db_installing), 
                          true);
              }
          }, "ProgressDialog");
      thread.start();
      restoreDBFromFile();
}

once again got no PD and got immediate crash:

04-12 08:40:38.894:
ERROR/AndroidRuntime(2448): ERROR:
thread attach failed 04-12
08:40:38.913: DEBUG/dalvikvm(2448):
LinearAlloc 0x0 used 639500 of 5242880
(12%) 04-12 08:40:39.094:
DEBUG/ddm-heap(2454): Got feature list
request 04-12 08:40:39.492:
WARN/dalvikvm(2454): threadid=15:
thread exiting with uncaught exception
(group=0x4001b188) 04-12 08:40:39.492:
ERROR/AndroidRuntime(2454): Uncaught
handler: thread ProgressDialog exiting
due to uncaught exception 04-12
08:40:39.504:
ERROR/AndroidRuntime(2454):
java.lang.RuntimeException: Can’t
create handler inside thread that has
not called Looper.prepare() 04-12
08:40:39.504:
ERROR/AndroidRuntime(2454): at
android.os.Handler.(Handler.java:121)
04-12 08:40:39.504:
ERROR/AndroidRuntime(2454): at
android.app.Dialog.(Dialog.java:105)
04-12 08:40:39.504:
ERROR/AndroidRuntime(2454): at
android.app.AlertDialog.(AlertDialog.java:63)
04-12 08:40:39.504:
ERROR/AndroidRuntime(2454): at
android.app.ProgressDialog.(ProgressDialog.java:80)
04-12 08:40:39.504:
ERROR/AndroidRuntime(2454): at
android.app.ProgressDialog.(ProgressDialog.java:76)
04-12 08:40:39.504:
ERROR/AndroidRuntime(2454): at
android.app.ProgressDialog.show(ProgressDialog.java:101)
04-12 08:40:39.504:
ERROR/AndroidRuntime(2454): at
android.app.ProgressDialog.show(ProgressDialog.java:90)
04-12 08:40:39.504:
ERROR/AndroidRuntime(2454): at
com.taxiorder.DBHelper$1.run(DBHelper.java:157)
04-12 08:40:39.504:
ERROR/AndroidRuntime(2454): at
java.lang.Thread.run(Thread.java:1096)

.
Tried a few more things with no result… So I just can’t figure out what’s wrong.

Once again – I just need to show PD while restoreDB() executing. I can’t run restoreDB() in separate thread because I need it finished before app can proceed. As I can see PD should be run in separate thread only but i can’t manage it.

Now I have 2 additional questions:

1) If a PD should be created in separate thread could it be created and showed if it’s thread doesn’t do anything at all. I mean something like this:

    new Thread(){
          public void run(){
              mProgressDialog = ProgressDialog.show(appContext, 
                      appContext.getResources().getString(R.string.progress_wait), 
                      appContext.getResources().getString(R.string.progress_db_installing), 
                      true);

      }.start();

As I understand the thread dies immediately after executing run() cause there is nothing to do and so PD doesn’t show.
2) PD by itself doesn’t suspend any thread while displaying, does it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:08:21+00:00Added an answer on May 21, 2026 at 9:08 am

    Try to declare AsyncTask as an inner class for the activity, instantiate and execute it there. I think the problem is in context given to ProgressDialog when you are creating it, your logic for doing this is too complicated. Declaring AsyncTask in activity is more common practice.

    Then, AsyncTask must do all you need with DB in doInBackground method

    public class SplashActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
    
            new RestoreDBTask().execute();
        }
    
    }
    
    private class RestoreDBTask extends AsyncTask <Void, Void, String>
    {
        private ProgressDialog dialog;
    
        @Override
        protected void onPreExecute()
        {
            dialog = ProgressDialog.show(
                SplashActivity.this,
                getString(R.string.progress_wait),
                getString(R.string.progress_db_installing), 
                true);
        }
    
        @Override
        protected String doInBackground(Void... params)
        {
            // do all the things with DB
            DBHelper dbHelper = new DBHelper(SplashActivity.this);
            dbHelper.close();
            return "";
        }
    
        @Override
        protected void onPostExecute(String result)
        {
            dialog.dismiss();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following need I have a logging table which logs som leads
I'm trying to do an app with listView, which is need to be filled
The requirements for this web app include the following: 1- Need to interact with
I need the following authentication script finished. I am weak at php/pdo so I
For a part of a program i need the following 2 methods. The first
I'm working on a project where I need the following. WCF service on the
can someone help with this? I need the following function to do this... $x
I'm having trouble converting a C# Linq statement to VB.NET. I need the following
I need to have following attribute value in my XML node: CommandLine=copy $(TargetPath) ..\..\&#x0D;&#x0A;echo
I'd need advice on following situation with Oracle/PostgreSQL: I have a db table with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.