Android的位置DistanceTo空 [英] Android Location DistanceTo null

查看:348
本文介绍了Android的位置DistanceTo空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 包com.competitivegamingaudio;
进口android.app.Activity;
进口android.content.Context;
导入android.location *。
进口android.os.Bundle;
进口android.util.Log;
导入android.widget *。
进口com.google.android.maps。* ;;公共类FindMyFriendsActivity延伸活动{
私有静态最后弦乐TAG =FindMyFriendsActivity;
公众的TextView LocationLabel;
公众的TextView DistanceLabel;
公共图形页面myMapView;
公共场所oldlocation;
公共场所currentlocation;@覆盖
公共无效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的);
    }@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    LocationLabel =(的TextView)findViewById(R.id.LocationLabel);
    DistanceLabel =(的TextView)findViewById(R.id.DistanceLabel);}LocationListener的LocationListener的=新LocationListener的()
{
公共无效onLocationChanged(地点)
    {
    currentlocation =新的位置(位置);
    LocationLabel.setText(location.toString());
    oldlocation =新的位置(位置);
    DistanceLabel.setText(距离+ calculatedistance(currentlocation,oldlocation));    }
公共字符串calculatedistance(位置A,位置B)
    {
    a.distanceTo(二);
    返回+ A;
    }
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员)
    {}
公共无效onProviderEnabled(字符串提供商)
    {    }
公共无效onProviderDisabled(字符串提供商)
{}};}

我无法得到这个code才能正常工作。我希望能够计算两个位置之间的距离。这是被传递到onLocationChanged方法的初始位置是工作(这证明我有合适的条件下),因为当我做 LocationLabel.setText(location.toString()); 可以正常工作没有得到任何空指针异常。然而,当 calculatedistance 方法被称为线49条, a.distanceTo(B); 行线路故障54.任何想法,为什么distanceTo方法不断失败?

错误消息:

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

main.xml中:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<的TextView
机器人:ID =@ + ID / LocationLabel
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/你好
/>
<的TextView
    机器人:ID =@ + ID / DistanceLabel
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/你好
/>
< / LinearLayout中>


解决方案

从我的Java code复制DistanceLabel并将其粘贴在main.xml中类似DistanceLabel解决。所有的人物出现在同一个,但它固定它。也许看着它在十六进制编辑器可以帮助,但现在我很高兴它的工作。感谢MisterSquonk。

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.

这篇关于Android的位置DistanceTo空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆