Android位置距离为null

时间:2012-01-09 23:54:48

标签: java android gps location

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)
{}

};

}

我无法让此代码正常运行。我希望能够计算两个位置之间的距离。传递给onLocationChanged方法的初始位置正在工作(证明我有正确的条件),因为当我LocationLabel.setText(location.toString());正常工作而没有获得任何空指针异常时。但是,当在第49行调用calculatedistance方法时,a.distanceTo(b);行在第54行失败。任何想法为什么distanceTo方法都会失败?

错误讯息:

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>

2 个答案:

答案 0 :(得分:1)

好的,关于你的代码我会改变很多东西,但这里不是这个地方。要尝试解决您的问题,请尝试此操作...

// package and imports here

public class FindMyFriendsActivity extends Activity {

    // TAG, TextViews, MapView, Locations as before but add
    // LocationManager and LocationListener here as below...
    LocationManager locationManager = null;
    LocationListener locationListener = null;

    @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);

        locationManager=(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        locationListener = new LocationListener() {
            // Put LocationListener methods here
        }
    }

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

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }
}

答案 1 :(得分:1)

通过从我的Java代码复制DistanceLabel并将其粘贴到main.xml中类似的DistanceLabel来解决。所有字符看起来都一样,但它修复了它。也许在十六进制编辑器中查看它会有所帮助但是现在我很高兴它正在工作。谢谢MisterSquonk。