gpt4 book ai didi

android - 获取我的位置没有用

转载 作者:行者123 更新时间:2023-11-29 21:13:19 25 4
gpt4 key购买 nike

我在获取我的位置时遇到空异常错误我在我的 list 中放了一个使用权限

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

任何人都可以帮助我弄清楚这有什么问题我是 android 开发的新手。提前致谢 。

代码:

    package com.example.locationsample;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
Button getloc;//`Location Button`
TextView lati;
TextView longi;
TextView address;

LocationManager location_manager;
String getLatitude;
String getLongitude;

double x;
double y;

Geocoder geocoder;
List<Address> addresses;
Location loc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getloc = (Button) findViewById(R.id.getlocation);
lati = (TextView) findViewById(R.id.latitude);
longi = (TextView) findViewById(R.id.longitude);
address = (TextView) findViewById(R.id.address);
location_manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

getloc.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
LocationListener listner = new MyLocationListner();
location_manager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, listner);

}
});

}
public class MyLocationListner implements LocationListener{


private Address location;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressWarnings("static-access")
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
getLatitude = "" + loc.getLatitude();
getLongitude = "" + loc.getLongitude();

lati.setText(getLatitude);
longi.setText(getLongitude);

x = location.getLatitude();
y = location.getLongitude();

try {
geocoder = new Geocoder(MainActivity.this, Locale.ENGLISH);
addresses = geocoder.getFromLocation(x, y, 1);
StringBuilder str = new StringBuilder();
if (geocoder.isPresent()) {
Toast.makeText(getApplicationContext(),
"geocoder present", Toast.LENGTH_SHORT).show();
Address returnAddress = addresses.get(0);

String localityString = returnAddress.getLocality();
String city = returnAddress.getCountryName();
String region_code = returnAddress.getCountryCode();
String zipcode = returnAddress.getPostalCode();

str.append(localityString + "");
str.append(city + "" + region_code + "");
str.append(zipcode + "");

address.setText(str);
Toast.makeText(getApplicationContext(), str,
Toast.LENGTH_SHORT).show();

} else {
Toast.makeText(getApplicationContext(),
"geocoder not present", Toast.LENGTH_SHORT).show();
}

// } else {
// Toast.makeText(getApplicationContext(),
// "address not available", Toast.LENGTH_SHORT).show();
// }
} catch (IOException e) {
// TODO Auto-generated catch block

Log.e("tag", e.getMessage());
}

}

@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub

}

}
}

最佳答案

你正在使用 loc 并且没有对它做任何事情,它是空的

在你的里面

@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
getLatitude = "" + loc.getLatitude();
getLongitude = "" + loc.getLongitude();
///etc..........
}

你应该使用 arg0

getLatitude = "" + arg0.getLatitude();
getLongitude = "" + arg0.getLongitude();

编辑:它实际上看起来与

相同
x = location.getLatitude();
y = location.getLongitude();

关于android - 获取我的位置没有用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22124295/

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