2

I have this code for managing some countrys on my database;

class checkCountryAsync extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
       super.onPreExecute();
       setContentView(R.layout.main);

    }
    @Override
    protected String doInBackground(String... aurl) {
        MDPIActivity.this.runOnUiThread(new Runnable() {

            public void run() {
                  CountryTable country = new CountryTable() ;
                  country.EnterCountry();
            }
        });
        return null;
    }
}

With this, I would like to set the content View and then in background, that the method onBackground works, but I have still to wait for the content view until the onBackground method is not finished.

Thank you.

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265
  • 2
    Is there a reason for putting setContentView(R.layout.main); in the AsyncTask and not in the OnCreate() of the Activity – Mohamed_AbdAllah Feb 20 '12 at 11:16
  • 1
    onBackground?? What a great name. – Paresh Mayani Feb 20 '12 at 11:21
  • What you want to implement exactly? – Paresh Mayani Feb 20 '12 at 11:22
  • @midoalageb, no, there is no reason, even if i try to set the content view in onCreate method, the result is still the same, I have to wait the time my runInbackground method is executed – Milos Cuculovic Feb 20 '12 at 11:25
  • @Ana : what do you want exactly to do ? if you want to update the UI during the processus of your task , so use the method : publishProgress() , and if you want to change the UI just after the method doInBackground have finished his work so use the onPostExecute() method, it's pretty simple :) – Houcine Feb 20 '12 at 11:29
  • @PareshMayani and Houcine I have an application which will start with checking if the country tables is empty or not, if empty, do something, if not, do nothing. The problem is that I would like to hawe my contentView already seted, before the check is done and actually, I have to wait for this with a black screen. – Milos Cuculovic Feb 20 '12 at 11:36
  • oh then why don't you setContentView() inside the onCreate() method itself. – Paresh Mayani Feb 20 '12 at 11:38
  • I have try this but still the same blacks screen and once the onBackground meethod has finished, then my content view is set – Milos Cuculovic Feb 20 '12 at 11:39

3 Answers3

2

i don't see any reason for putting the setContentView() in the onPreExecute method, it should be in the onCreate method to avoid any kind of NullPointerException when you will try to find your views, and for your AsyncTask , you should just use the onPostExecute() which is executed after the method doInBackground()

Flexo
  • 87,323
  • 22
  • 191
  • 272
Houcine
  • 24,001
  • 13
  • 56
  • 83
  • Thanks but when I am setting my content view in oncreate method, I have to wait that my onBackground function is finished and I don't know why. – Milos Cuculovic Feb 20 '12 at 11:29
  • @Ana : what do you want exactly to do ? if you want to update the UI during the processus of your task , so use the method : publishProgress() , and if you want to change the UI just after the method doInBackground have finished his work so use the onPostExecute() method, it's pretty simple :) . see my edits – Houcine Feb 20 '12 at 11:30
  • Also, I have an application which will start with checking if the country tables is empty or not, if empty, do something, if not, do nothing. The problem is that I would like to hawe my contentView already seted, before the check is done and actually, I have to wait for this with a black screen. – Milos Cuculovic Feb 20 '12 at 11:35
  • it's simple Ana , you should set the contentView in the onCreate mthod ,and then when you just start your Task for checking the country ...etc, just display a ProgressDialog , to tell the user to wait until the checking is finished, and then , from the method doInBackground , you will return a boolean ( true if the country exist , and false if not ) , and in your method onPostExecute() , you need just to interacte and modify your UI in function of the boolean returned from the doInBackground method :) – Houcine Feb 20 '12 at 11:38
  • In fact, this is not the goal of this, I am trying to do the country checking realy on the bacground, so the user will ot know that the application will check the country presenc. He should be able to work as nothing hapens. – Milos Cuculovic Feb 20 '12 at 11:41
  • so do it in the background without displaying any progressDialog (which is not prefereable ) and then , when the treatment is finished, just do what you want in the onPostExecute() , remember that you should ALWAYS set the contentView in the onCreate() method , if you want , just upload your project , and tell me exactly what you want, and i will correct the bug for you this evening – Houcine Feb 20 '12 at 11:45
  • Thanks so much, what I am not understanding is that even if I try to set the content view in onCreate, I have still to wait with the black screen. In which concerns the project, I have more than 30 classes inside and it will not be easy to explain what i am doing inside. But thanks soo much – Milos Cuculovic Feb 20 '12 at 11:52
  • just set your contentView in the onCreate with some views inside it , and then try to launch your Task in the onResume method, and see what it will gives you , and for me , there is no problem if the project contains 30 or 40 classes :) , you will tell me which activities you have a problem with it , and i will do the necessary :) – Houcine Feb 20 '12 at 13:51
  • Ok great, how can I send you the project? – Milos Cuculovic Feb 20 '12 at 13:53
  • just upload it in www.mediafire.com and send me back the link via Email ( you will find my email in my profile page ) – Houcine Feb 20 '12 at 14:18
  • this is it : kicha.lahoucine@gmail.com – Houcine Feb 20 '12 at 15:16
  • Thanks, i just sent you the project. Could you invite me on private chat please, than will be easier – Milos Cuculovic Feb 20 '12 at 15:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7965/discussion-between-ana-and-houcine) – Milos Cuculovic Feb 20 '12 at 15:44
0

Please try this one, think its work.`class checkCountryAsync extends AsyncTask {

@Override
protected void onPreExecute() {
   super.onPreExecute();
   setContentView(R.layout.main);

}
@Override
protected String doInBackground(String... aurl) {
    CountryTable country = new CountryTable() ;
    country.EnterCountry();
    return null;
}

} `

Dhaval
  • 553
  • 3
  • 9
  • Thanks, but the problem is that there I will get an exception because I will got this exception: http://stackoverflow.com/questions/9357513/cant-create-handler-inside-thread-that-has-not-called-looper-prepare-with-asy – Milos Cuculovic Feb 20 '12 at 11:30
  • @Ana : as the answer said , in your method EntryCountry() , you try to modify the UI , from a NON UI Thread, the Only thread that can update the User Interface is the UIThread , that's why you got that exception – Houcine Feb 20 '12 at 11:35
  • Thanks but I don't understand where I am touching the UI because on my EntryCountry() method, I am only inserting country on my local database. – Milos Cuculovic Feb 20 '12 at 11:38
0

You should try publishProgress() and onProgressUpdate() methods.

Arun Badole
  • 10,977
  • 19
  • 67
  • 96