gpt4 book ai didi

java - Android Location Distance为空

转载 作者:行者123 更新时间:2023-11-30 09:41:59 25 4
gpt4 key购买 nike

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 方法的初始位置有效(这证明我有正确的条件),因为当我执行空指针异常。但是,当在第 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)

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

最佳答案

好的,关于您的代码,我要更改很多地方,但这里不是地方。要尝试解决您的问题,试试这个...

// 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);
}
}

关于java - Android Location Distance为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8796849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com