-1

I was sitting 5 hours on one code with AyncTask which was not running properly. I just created another simple Activity (because in last one onPostExecute() wasn't working) and now this simple Activity is also not starting the AsyncTask. Can anyone see what I'm doing wrong?

public class ServerStatus extends Activity {
Context context;    
private ProgressDialog pd;  
int a;  
TextView test;  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.server_status);
        context=this;
        test=(TextView) findViewById(R.id.welcomemessage);
        new Download().execute();


    }

    public class Download extends AsyncTask<Void, Void, Void>{

        protected Void onPreExecute(Void... arg0) {
            pd = new ProgressDialog(context);
            pd.setTitle("Processing...");
            pd.setMessage("Please wait.");
            pd.setCancelable(false);
            pd.setIndeterminate(true);
            pd.show();
            return null;
        }


        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            a++;
            return null;
        }


        protected Void onPostExecute(Void... arg0) {
            if (pd!=null) 
                pd.dismiss();
            test.setText(a);
            return null;
        }

    } 

}

Also, does NavigationDrawer block UI thread? Because I can't even update TextView when I implement it.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
Darknez
  • 39
  • 7
  • You should be doing UI component modifications from within the onProgressUpdate(). Navigation drawers do not block the UI thread. What occurs when you try and run it? – Jay Snayder Sep 30 '14 at 20:10
  • Here's my [answer to a similar question][1] [1]: http://stackoverflow.com/questions/25752210/cant-see-progressdialog-while-asynctask-run-in-background/25752553#25752553 – user3144836 Sep 30 '14 at 20:14
  • Well, i was updating UI Thread from onPostExecute() so maybe there was my problem (it was not updating anything or even doing anything like it wasn't called). Gonna give it a try tommorow then. Thanks for suggestion. – Darknez Sep 30 '14 at 20:15
  • 1
    You should really use the @Override annotation. Your compiler would tell you the issue right away. – njzk2 Sep 30 '14 at 20:26

2 Answers2

2

The methods aren't correct. You should add the @Override annotation to them so it will yell at you when doing it wrong.

onPreExecute() doesn't take any params so it should be

@Override
protected Void onPreExecute() {

also change the param type of `onPostExecute() to

@Override
 protected Void onPostExecute(Void arg0) {

Remove the "..." See Varargs for an explanation.

Docs

Post explaining AsyncTask and getting values

those params in the class declaration are for doInBackground(), onProgressUpdate(), and onPostExecute()

As far as the NavDrawer, I'm not sure what issue you are having with that.

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Seems to be working but probably i did something wrong because after adding @Override to onPostExecute() it says "The method onPostExecute(Void...) of type ServerStatus.download must override or implement a supertype method". Probably i messed something with arguments but i would be grateful if you or another person would correct me. After 5 hrs im not thinking straight... – Darknez Sep 30 '14 at 20:12
-1

You need to change your code in onPreExecute

pd = new ProgressDialog(context);

to

pd = new ProgressDialog(getActivity());
anetha
  • 118
  • 2
  • 6