开发者

Android Why does my app Force to close?

开发者 https://www.devze.com 2023-01-25 17:23 出处:网络
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()

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();
            }

       开发者_Go百科     @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 ?


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);
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号