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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:54:20+00:00 2026-06-04T20:54:20+00:00

I have a simple activity in which I start an AsyncTask with a button

  • 0

I have a simple activity in which I start an AsyncTask with a button and stop it with another.

In AsyncTask I use a LocationListner to get updates from the GPS sensor and calculate the distance between obtained Locations.

When I press the start button, new thread is created and the code works fine. In the debug perspective I can see that GPS updates are caught regularly and my variables are updated accordingly.

However, the problem starts when I press the cancel button. In the documentation (http://developer.android.com/reference/android/os/AsyncTask.html) I have read that cancel() method should invoke onCancelled() when doInBackground code finishes. For that reason I have put:

if (isCancelled()){
    locManager.removeUpdates(locListener);
}

all over the doInBackground method, hoping that it will get me out of it. Unfortunately, I don’t even get to that part. Once when I press the end button I get the following error:

05-22 11:10:05.043: E/Handler(6353): java.lang.NullPointerException
05-22 11:10:05.043: E/Handler(6353):    at com.pavle.taximetar.TaximetarActivity$2.onClick(TaximetarActivity.java:43)
05-22 11:10:05.043: E/Handler(6353):    at android.view.View.performClick(View.java:3538)
05-22 11:10:05.043: E/Handler(6353):    at android.view.View$PerformClick.run(View.java:14330)
05-22 11:10:05.043: E/Handler(6353):    at android.os.Handler.handleCallback(Handler.java:607)
05-22 11:10:05.043: E/Handler(6353):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-22 11:10:05.043: E/Handler(6353):    at android.os.Looper.loop(Looper.java:154)
05-22 11:10:05.043: E/Handler(6353):    at android.app.ActivityThread.main(ActivityThread.java:4974)
05-22 11:10:05.043: E/Handler(6353):    at java.lang.reflect.Method.invokeNative(Native Method)
05-22 11:10:05.043: E/Handler(6353):    at java.lang.reflect.Method.invoke(Method.java:511)
05-22 11:10:05.043: E/Handler(6353):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-22 11:10:05.043: E/Handler(6353):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-22 11:10:05.043: E/Handler(6353):    at dalvik.system.NativeStart.main(Native Method)

Any idea, what’s behind this error. Here is the full code. (Build target is Android 4.0.3). The code is tested on HTC One V.

public class TaximetarActivity extends Activity {

DistanceCalculator distanceCalculator;
public ArrayList<Drive> listOfRides = new ArrayList<Drive>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button startRide = (Button) findViewById(R.id.button1);
    final Button endRide = (Button) findViewById(R.id.button2);

    startRide.setOnClickListener(new OnClickListener() {        
        public void onClick(View v) {
            distanceCalculator = new DistanceCalculator();
            distanceCalculator.execute(getApplicationContext());
        }   
    });

    endRide.setOnClickListener(new OnClickListener() {  
        public void onClick(View v) {
            distanceCalculator.cancel(true);
        }
    });
}
}

public class DistanceCalculator extends AsyncTask<Context, Void, Void> {

Drive currentRide = new Drive();
Float distanceOfaRide = (float) 0;
LocationManager locManager;
LocationListener locListener;
Context mCtx;
boolean startPointEntered = false;

@Override
protected Void doInBackground(Context... params) {

    mCtx = params[0];


    Looper.prepare();

    if (isCancelled()){
        locManager.removeUpdates(locListener);
    }

    locManager = (LocationManager) mCtx.getSystemService(Context.LOCATION_SERVICE);
    locListener = new LocationListener() {

        public void onStatusChanged(String provider, int status, Bundle extras) {
            if (isCancelled()){
                locManager.removeUpdates(locListener);
            }
        }

        public void onProviderEnabled(String provider) {
            if (isCancelled()){
                locManager.removeUpdates(locListener);
            }
        }

        public void onProviderDisabled(String provider) {
            if (isCancelled()){
                locManager.removeUpdates(locListener);
            }               
        }

        public void onLocationChanged(Location location) {
            if (DistanceCalculator.this.isCancelled()){
                locManager.removeUpdates(locListener);
            }
            calculateDistance(location);
        }
    };

    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, locListener);
    Looper.loop();
    return null;
}

@Override
protected void onCancelled() {
    locManager.removeUpdates(locListener);
    Intent intent = new Intent(mCtx, DisplayRide.class);
    intent.putExtra("Distance", distanceOfaRide);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mCtx.startActivity(intent);
}

private void calculateDistance(Location location){
    if (startPointEntered){
        Location lastPoint = currentRide.drivePoints.get(currentRide.drivePoints.size()-1);
        currentRide.addNewPoint(location);
        distanceOfaRide = distanceOfaRide + location.distanceTo(lastPoint);
    }
    else{
        currentRide.setStartPoint(location);
        startPointEntered = true;
    }
}
}

public class Drive {

    Location startPoint;
    Location endPoint;
    ArrayList<Location> drivePoints = new ArrayList<Location>();

    public Drive() {
        // TODO Auto-generated constructor stub
    }

    public void setStartPoint(Location location){
            startPoint = new Location(location);
        drivePoints.add(0, startPoint);
    }
    public void setEndPoint(Location location){
        endPoint = new Location(location);  
    }
    public void addNewPoint(Location location){
        drivePoints.add(location);
    }
    public Location getStartPoint(){
        return startPoint;
    }
}
  • 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-04T20:54:21+00:00Added an answer on June 4, 2026 at 8:54 pm

    Problem was in this part of the code:

    endRide.setOnClickListener(new OnClickListener() {  
        public void onClick(View v) {
            distanceCalculator.cancel(true);
        }
    });
    

    Here, it is required to send a message to a handler which is created in the Async task. In response to that message, the message handler function should cancel the task.

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

Sidebar

Related Questions

I have a simple game activity. I start it and than press the back
friends, i have a EditText on simple activity with a button. when every i
I have a simple screen with a start button. When the Start button is
I have an application that contains a main Activity, which has a very simple
I have an activity which displays some data fetched from the server. If no
I have a sax parser which runs when the user taps a start button.
I have an Activity which uses a Fragment . I simply want to pass
I have an Activity, which simply consists in listing Pair<String, String> objects. I have
So I have a very simple android activity that starts a timer when you
This seems like a simple problem: I have a WF4 activity that guides the

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.