Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android位置距离为空_Java_Android_Gps_Location - Fatal编程技术网

Java Android位置距离为空

Java Android位置距离为空,java,android,gps,location,Java,Android,Gps,Location,我很难让这个代码正常工作。我希望能够计算两个位置之间的距离。传递给onLocationChanged方法的初始位置正在工作(这证明我有正确的条件),因为当我执行LocationLabel.setText(location.toString())时可以正常工作,而不会出现任何空指针异常。但是,当在第49行中调用calculatedInstance方法时,a.distanceTo(b)第54行失败。知道为什么distanceTo方法一直失败吗 错误消息: package com.competitiv

我很难让这个代码正常工作。我希望能够计算两个位置之间的距离。传递给onLocationChanged方法的初始位置正在工作(这证明我有正确的条件),因为当我执行
LocationLabel.setText(location.toString())时可以正常工作,而不会出现任何空指针异常。但是,当在第49行中调用
calculatedInstance
方法时,
a.distanceTo(b)第54行失败。知道为什么distanceTo方法一直失败吗

错误消息:

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

};

}
main.xml:

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

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

在你的代码中,旧位置和当前位置是相同的??发布日志,显示异常01-09 18:42:06.342:ERROR/AndroidRuntime(7891):致命异常:main 01-09 18:42:06.342:ERROR/AndroidRuntime(7891):java.lang.NullPointerException 01-09 18:42:06.342:ERROR/AndroidRuntime(7891):在com.competitivegamingaudio.FindMyFriendsActivity$1.onLocationChanged(FindMyFriendsActivity.java:49)01-09 18:42:06.342:ERROR/AndroidRuntime(7891):在android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)等处。旧位置和当前位置故意相同。最初我用白宫的经纬度作为旧位置,但我得到了同样的错误。不,那不起作用。问题不在于locationManager或locationListener为null(它们不是)。它告诉我oldlocation是空的。问题是当你发布logcat时,你只发布了几行,然后用“etc”结束,这真的没有帮助。如果我注释掉DistanceLabel.setText(“距离:+CalculatedInstance(currentlocation,oldlocation)),则张贴显示“原因”的位然后程序运行正常。但是仍然不能帮助我获得两个位置之间的距离。正如我上面所说的,发布完整的logcat异常,包括'Caused by'部分,作为对您的问题的编辑。没有'Caused by'子句。这很奇怪,因为我以前在遇到其他错误时也看到过。
// 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);
    }
}