1

I admit, I'm new at this whole Android stuff. I am trying to make an app but randomly I get Force close errors and I really don't know why. My application has many activities, none of them finish() when I start a new one. I get data from the web (via web services and direct image downloading) and I use AsyncTask a lot. Most of the time it crashes on the asynctask. Here is a sample on how I do things:

private BackTask backTask;
Activity ctx = this;

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.trackslist);

        backTask = new BackTask();
            backTask.execute();
    }

    protected class BackTask extends AsyncTask<Context, String, myObject>
        {
            @Override
            protected myObject doInBackground(Context... params) 
            {
                 try{
                        if (hasInternet(ctx)==true)
                        {
                            //access the web SERVICE here
                        //initialize myObject WITH result FROM the web
                         return myObject
                       }
                       else
                       {
                            return null
                       }

                  }catch(Exception ex){
                       return null;
                }
            }

            @Override
            protected void onPreExecute()
            {
                    super.onPreExecute();
            }

            @Override
            protected void onProgressUpdate(String... values) 
            {
                    super.onProgressUpdate(values);
            }

            @Override
            protected void onCancelled()
            {
                    super.onCancelled();
            }

            @Override
            protected void onPostExecute( myObject result ) 
            {
                    super.onPostExecute(result);
                    if (result==null || result.isEmpty())
                    {
                           //no valid result, show a message
                    }
                    else
                    {
                        //result valid do something with it
                    }
            }
        }

     @Override
        public void onPause()
        {
            if (backTask!=null && ! backTask.isCancelled())
            {
                backTask.cancel(true);
            }
            super.onPause();
        }

    public void btnStartOnClick(View target) {
          Intent intent = new Intent(this, MyNewActivity.class); 
      startActivity(intent);
 }

When the activity gets onPause() the task is being canceled. I am not sure what happens during the try/catch if a error appears, from what I've did, it should return null, but I think here I miss something. As I said before, randomly I get a force close even if I am on another Activity. This is really frustrating as I can't offer a app that has this behavior. So, what am I doing wrong ?

Alin
  • 14,809
  • 40
  • 129
  • 218
  • if there's a FC, you should check the log ('adb shell logcat' in command line from sdk/tools directory; or in Eclipse DDMS view). Then paste the error log here. – Mathias Conradt Nov 19 '10 at 06:30
  • I checked the log and variably it is null pointer exception in the asynctask... the thing is I'm on another activity when I get the message. – Alin Nov 19 '10 at 07:14

2 Answers2

2

There is problem in your code. I have corrected as follows: You find I have added this while calling async task. Your async task accept context as argument and you was not passing that.

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.trackslist);

     backTask = new BackTask();
     backTask.execute(this);
}
Umakant Patil
  • 2,227
  • 6
  • 32
  • 58
  • I never used execute(this). Curiously I didn't manage to find any info if it's requested or not. Could you please elaborate a little why is it necessary ? Thank you. – Alin Nov 19 '10 at 07:21
  • Alin, Its not necessary. You have defined Context as first parameter "extends AsyncTask" so you need to pass it. If you don't need to pass Anything then put there "Void" as first paramteter – Umakant Patil Nov 19 '10 at 07:32
  • I understand now, good point. Interesting that it works like it is. What happens in a try catch if something goes wrong ? Does it return null to onPostExecute ? – Alin Nov 19 '10 at 07:43
0

You need to ask inside your AsyncTask class for isCancelled() and then decide what to do.

Check this question. It has a good explanation by Romain Guy:

You can stop an AsyncTask. If you call cancel(true), an interrupt will be sent to the background thread, which may help interruptible tasks. Otherwise, you should simply make sure to check isCancelled() regularly in your doInBackground() method. You can see examples of this at code.google.com/p/shelves.

Community
  • 1
  • 1
Macarse
  • 91,829
  • 44
  • 175
  • 230