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>
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.