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

  • Home
  • SEARCH
  • 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 7037417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:32:33+00:00 2026-05-28T01:32:33+00:00

package com.competitivegamingaudio; import android.app.Activity; import android.content.Context; import android.location.*; import android.os.Bundle; import android.util.Log; import android.widget.*;

  • 0
package com.competitivegamingaudio;
import android.app.Activity;
import android.content.Context;
import android.location.*;
import android.os.Bundle;
import android.util.Log;
import android.widget.*;
import com.google.android.maps.*;;

public class FindMyFriendsActivity extends Activity {


private static final String TAG = "FindMyFriendsActivity";
public TextView LocationLabel;
public TextView DistanceLabel;
public MapView myMapView;
public Location oldlocation;
public Location currentlocation;

@Override
public void onResume()
    {
    super.onResume();
    LocationManager locationManager=(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
    }

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LocationLabel=(TextView) findViewById(R.id.LocationLabel);
    DistanceLabel=(TextView)findViewById(R.id.DistanceLabel);



}

LocationListener locationListener =new LocationListener()
{
public void onLocationChanged (Location location)
    {
    currentlocation=new Location(location);
    LocationLabel.setText(location.toString());
    oldlocation=new Location(location);
    DistanceLabel.setText("Distance: "+calculatedistance(currentlocation,oldlocation));

    }
public String calculatedistance(Location a, Location b)
    {
    a.distanceTo(b);
    return ""+a;
    }
public void onStatusChanged(String provider, int status, Bundle extras)
    {}
public void onProviderEnabled(String provider)
    {

    }
public void onProviderDisabled(String provider)
{}

};

}

I’m having trouble getting this code to work properly. I want to be able to calculate the distance between two Locations. The initial location that’s being passed to the onLocationChanged method is working (which proves I have the right conditions), because when I do LocationLabel.setText(location.toString()); that works properly without getting any null pointer exceptions. However, when the calculatedistance method is called in line 49, the a.distanceTo(b); line fails on line 54. Any idea why the distanceTo method keeps failing?

Error Message:

01-09 20:11:58.085: ERROR/AndroidRuntime(9716): java.lang.NullPointerException
01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at com.competitivegamingaudio.FindMyFriendsActivity$1.onLocationChanged(FindMyFriendsActivity.java:47)
01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)
01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at android.os.Looper.loop(Looper.java:130)
01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at android.app.ActivityThread.main(ActivityThread.java:3821)
01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at java.lang.reflect.Method.invoke(Method.java:507)
01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at dalvik.system.NativeStart.main(Native Method)

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView  
android:id="@+id/LocationLabel"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/> 
<TextView  
    android:id="@+id/DistanceLabel"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/>
</LinearLayout>
  • 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-28T01:32:34+00:00Added an answer on May 28, 2026 at 1:32 am

    Solved by copying DistanceLabel from my Java code and pasting it over the similar DistanceLabel in main.xml. All the characters appear the same but it fixed it. Perhaps looking at it in a hex editor would help but for now I’m just glad it’s working. Thanks MisterSquonk.

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

Sidebar

Related Questions

package com.android.SMStest; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.Button;
package com.rest; import java.io.IOException; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button;
package com.geoo; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle;
package com.VRG; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.content.Context; import android.media.MediaPlayer;
package com.commonsware.android.layouts; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import java.util.Date; public class
package com.ustr.eMIRnew; import java.util.ArrayList; import java.util.HashMap; import android.R; import android.app.Activity; import android.os.Bundle; import android.widget.ListView;
package com.duncan.hello.world; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;
package com.ebonybutler.cexample3; import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener;
package com.example.Calc; import android.R.string; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter;
package com.example.android.home; import android.app.Activity; import android.os.Bundle; import android.widget.*; import android.view.*; public class HomeActivity extends

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.