0

こんにちは、正確な位置情報を取得したいので、次のリンクを使用しました

Androidでユーザーの現在地を取得する最も簡単で堅牢な方法は何ですか?

以下はそのサイトのコードです:

      LocationResult locationResult = new LocationResult(){
    @Override
    public void gotLocation(Location location){
        //Got the location!
    }
   };

MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);

クラスで私LocationResultはそれを変更し、テキストビューでLOCATIONを表示したいので、以下のコードを書きました

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){
            TextView txtValue = (TextView)findViewById(R.id.txtValue);
            txtValue.setText("LATITUDE : - "+location.getLatitude()+"\n\n LONGITUDE :- "+location.getLongitude()+"\n\n ALTITUDE :- "+location.getAltitude());
        }
    };
    myLocation = new MyLocation();
    myLocation.getLocation(this, locationResult);

}

しかし、その中で私は次のような例外を取得しています

以下は私のlogcatです

 10-04 17:06:35.699: E/AndroidRuntime(5733): FATAL EXCEPTION: Timer-0
 10-04 17:06:35.699: E/AndroidRuntime(5733): java.lang.NullPointerException
 10-04 17:06:35.699: E/AndroidRuntime(5733):    at    com.example.gps_accurate_data.MainActivity$1.gotLocation(MainActivity.java:22)
 10-04 17:06:35.699: E/AndroidRuntime(5733):    at com.example.gps_accurate_data.MyLocation$GetLastLocation.run(MyLocation.java:119)
 10-04 17:06:35.699: E/AndroidRuntime(5733):    at java.util.Timer$TimerImpl.run(Timer.java:284)
4

2 に答える 2

1

提供したリンクを見ると、デバイスが場所を取得しようとすると、コードが実行されます。

if(gps_loc!=null){
    locationResult.gotLocation(gps_loc);
    return;
}

if(net_loc!=null){
    locationResult.gotLocation(net_loc);
    return;
}

locationResult.gotLocation(null);

nullつまり、場所の取得に失敗した場合、場所が に設定された状態で電話をかける可能性があります。それを確認する必要があります。

于 2012-10-04T11:56:18.400 に答える
0

上部でアクティビティを宣言します。TextView txtValue;

次に、あなたのonCreate()txtValue = (TextView)findViewById(R.id.txtValue);

if(location != null){
    txtValue.setText("LATITUDE : - "+location.getLatitude()+"\n\n LONGITUDE :-    "+location.getLongitude()+"\n\n ALTITUDE :- "+location.getAltitude());
}
于 2012-10-04T11:50:33.347 に答える