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 3425788
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:38:09+00:00 2026-05-18T06:38:09+00:00

I admit, I’m new at this whole Android stuff. I am trying to make

  • 0

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 ?

  • 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-18T06:38:10+00:00Added an answer on May 18, 2026 at 6:38 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'll admit that this is a homework assignment, but I'm not asking you
I must admit that I am new to jQuery and JS but I really
I must admit I'm having trouble formulating this question, but I'll try my best
I admit that this question is subjective but I am interested in the view
I admit, I'm noob when it comes to sourceforge. I'm trying to get the
I hate to admit it but I'm stuck trying to figure out how to
I admit this is not strictly a programming question, although I do use my
Background I admit, this question stems from an ultimate lack of deep understanding of
OK so I admit right off the top that this is a bit screwy
I'll admit the title is a bit confusing but it was hard to come

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.