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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:28:38+00:00 2026-06-08T06:28:38+00:00

I am trying to follow the following pattern to secure my background tasks when

  • 0

I am trying to follow the following pattern to secure my background tasks when an activity is destroyed. Since I wanted to reuse my tasks in different activities, I implemented a “TaskMethod” class for each task.

public class TaskMethod
{
   private SomeActivity mAct_ = null;
   private MyTask mTask_ = null;

    public TaskMethod(SomeActivity act)
    {
        mAct_ = act;
    }

    public void execute()
    {
           mTask_ = (MyTask) mAct_.getLastNonConfigurationInstance();

           if (mTask_ != null)
           {
                mTask_.activity_ = mAct_;

                if (mTask_.isFinished_)
                   mTask_.updateUI();
           }
           else
           {
                mTask_ = new MyTask();
                mTask_.activity_ = mAct_;
                mTask_.execute();
           }
    }

    public void cleanUp()
    {
           myTask_.activity_ = null;

           if (mAct_.isFinishing())
            myTask_.cancel(false);
    }

    static class MyTask extends AsyncTask<Void, Void, String>
    {

       @Override
        protected String doInBackground(Void... params)
        {
            ...
        }

    @Override
    protected void onPostExecute(String result) 
        {
            ...
            isFinished_ = true;
            updateUI();
        }

        public void updateUI()
        {
            if (activity_ != null)
            {
                ...
            }            
        }

        SomeActivity activity_ = null;
        boolean isFinished_  = false;
}

So, each time I want to use a task in an activity, I declare it

public class SomeActivity {

private TaskMethod task_;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
{
        super.onCreate(savedInstanceState);

        task_ = new TaskMethod(this);
        task_.execute();
    }

@Override
public void onDestroy()
{
    super.onDestroy();

    task_.cleanUp();
}

}

However, when my activity closes, I get the following exception:

07-21 19:37:15.195: E/AndroidRuntime(25724): FATAL EXCEPTION: main
07-21 19:37:15.195: E/AndroidRuntime(25724): java.lang.RuntimeException: Unable to destroy activity 
{com.signals.signals/com.signals.signals.activity.PreferencesActivity}: java.lang.NullPointerException
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3108)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3126)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.ActivityThread.access$1200(ActivityThread.java:122)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1179)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.os.Looper.loop(Looper.java:137)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.ActivityThread.main(ActivityThread.java:4340)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at java.lang.reflect.Method.invokeNative(Native Method)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at java.lang.reflect.Method.invoke(Method.java:511)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at dalvik.system.NativeStart.main(Native Method)
07-21 19:37:15.195: E/AndroidRuntime(25724): Caused by: java.lang.NullPointerException
07-21 19:37:15.195: E/AndroidRuntime(25724):    at com.signals.signals.activity.tasks.CitiesTaskMethod.cleanUp(CitiesTaskMethod.java:92)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at com.signals.signals.activity.PreferencesActivity.onDestroy(PreferencesActivity.java:117)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.Activity.performDestroy(Activity.java:4629)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1079)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3095)

What am I doing wrong?? Thanks!

  • 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-06-08T06:28:39+00:00Added an answer on June 8, 2026 at 6:28 am

    It seems like when you’re calling cleanUp() on the task_ it is null, so you just need to null-check the variable before making that call. Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to follow the steps on the following site http://msdn.microsoft.com/en-us/library/ms181089(VS.80).aspx But I
I am trying to follow the short example in the following answer on using
I am trying to find directories that follow the below pattern and any instances
I have the following Linux directions I am trying to follow and am looking
I'm trying to follow Sun's JDBC tutorial at http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html It gives the following example
Trying to follow a technique found at bzr and gitosis I did the following:
I am trying to follow this tutorial and I get the following error which
I am trying to follow this example: http://msdn.microsoft.com/en-us/library/aa179614%28SQL.80%29.aspx# It says to add the following
I have several UIView subclasses (buttons, labels, etc.) that follow the following setup pattern.
I'm trying to follow the following post to build a c# app with mono

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.