获取纬度和经度?
Posted
技术标签:
【中文标题】获取纬度和经度?【英文标题】:Getting Latitude and Longitude? 【发布时间】:2019-03-25 18:23:06 【问题描述】:我正在尝试在我的 RecyclerView 中获取按钮单击位置。
但是当我点击按钮时,应用程序崩溃给我这个错误
日志猫:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.location.Location android.location.LocationManager.getLastKnownLocation(java.lang.String)' on a null object reference
RecyclerView 适配器代码:
public class RecieverAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Filterable
Context context;
List<BloodData> data = Collections.emptyList();
List<BloodData> searchData;
LayoutInflater inflater;
RecieverAdapter RAdapter;
LocationManager locationManager;
public RecieverAdapter(RecieveBlood recieveBlood, List<BloodData> data)
this.context = recieveBlood;
inflater = LayoutInflater.from( context );
this.data = data;
this.RAdapter = this;
searchData = new ArrayList<>( data );
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
View view = inflater.inflate( R.layout.blood_list, viewGroup, false );
MyHolder holder = new MyHolder(view);
return holder;
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i)
MyHolder myHolder = (MyHolder) viewHolder;
BloodData current = data.get( i );
myHolder.Sr_Num.setText( " "+i );
myHolder.Donar_Name.setText( "Name : "+current.getD_Name() );
myHolder.Donar_Blood_Group.setText( "Blood Group : " +current.getD_blood_group());
myHolder.Donar_Mobile.setText( "Mobile : "+current.getD_Number());
myHolder.Donar_Gender.setText( "Gender : "+current.getD_Gender());
myHolder.location1.setOnClickListener( new View.OnClickListener()
@Override
public void onClick(View v)
getLocation();
);
void getLocation()
if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions( (Activity) context, new String[]android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION, 101);
else
Location location = locationManager.getLastKnownLocation( LocationManager.NETWORK_PROVIDER );
if(location != null)
double lati = location.getLatitude();
double longi = location.getLongitude();
Log.e(TAG,"Location :" +lati+ " ,"+longi );
@Override
public int getItemCount()
return data.size();
private class MyHolder extends RecyclerView.ViewHolder
TextView Sr_Num,Donar_Name,Donar_Blood_Group,Donar_Mobile,Donar_Gender;
Button location1, location2;
public MyHolder(@NonNull View itemView)
super( itemView );
Sr_Num = (TextView) itemView.findViewById( R.id.sr_number );
Donar_Name = (TextView) itemView.findViewById( R.id.Donar_Name );
Donar_Blood_Group =(TextView) itemView.findViewById( R.id.Donar_blood_Group);
Donar_Mobile = (TextView) itemView.findViewById( R.id.Donar_Number );
Donar_Gender = (TextView) itemView.findViewById( R.id.Donar_Gender );
location1 = (Button) itemView.findViewById( R.id.Location1 );
location2 = (Button) itemView.findViewById( R.id.location2 );
我设置了该位置所需的所有权限。
Android MainFest 文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nikhil.bloodbank.bloodsupplies">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
所以每当我点击按钮时,应用程序崩溃都会给我这个错误。 而且我还手动检查了是否授予权限。
【问题讨论】:
它可以帮助:***.com/questions/32290045/… 【参考方案1】:您尚未初始化您的位置管理器实例。 如下更改您的适配器构造函数:
public RecieverAdapter(RecieveBlood recieveBlood, List<BloodData> data)
this.context = recieveBlood;
inflater = LayoutInflater.from( context );
this.data = data;
this.RAdapter = this;
searchData = new ArrayList<>( data );
//Initialize Location manager
locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
请注意,使用 getLastKnownLocation()
可能会返回一个旧的和过时的位置,如 android 文档中所述:
返回一个 Location 指示来自最后一个已知位置的数据 从给定的提供程序获得的修复。
这可以在不启动提供程序的情况下完成。请注意,这 位置可能已过时,例如,如果设备被转动 关闭并移动到另一个位置。
如果提供者当前被禁用,则返回 null。
在下面的链接中,有多种获取位置的方法有很好的解释: https://developer.android.com/guide/topics/location/strategies
更新
我在下面添加了 locationListener 逻辑,但最佳做法是将获取位置的代码放在 Activity 或 Fragment 中,请记住,当 Activity 或 Fragment 关闭时,应删除 locationListener。使用locationManager.removeUpdates(locationListener)
来删除locationListener。
void getLocation()
if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions( (Activity) context, new String[]android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION, 101);
else
Location location = locationManager.getLastKnownLocation( LocationManager.NETWORK_PROVIDER );
if(location != null)
double lati = location.getLatitude();
double longi = location.getLongitude();
Log.e(TAG,"Location :" +lati+ " ,"+longi );
else
boolean isNetworkAvailable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(isNetworkAvailable) fetchCurrentLocation()
else //Redirect user to device settings in order to turn on location service
你应该如下实现locationListener:
fetchCurrentLocation()
try
LocationListener locationListener = new LocationListener()
@Override
public void onLocationChanged(final Location location)
if (location != null)
locationManager.removeUpdates(this);
double lati = location.getLatitude();
double longi = location.getLongitude();
Log.e(TAG,"Location :" +lati+ " ,"+longi );
@Override
public void onProviderDisabled(String s)
locationManager.removeUpdates(this);
@Override
public void onStatusChanged(String s, int i, Bundle bundle)
@Override
public void onProviderEnabled(String s)
;
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500L,30f, locationListener);
catch (SecurityException e)
e.printStackTrace();
【讨论】:
我已经初始化了它但是它仍然不工作应用程序没有崩溃但我没有得到经度和纬度。 正如我提到的getLastKnownLocation()
可能不会返回任何位置,那是因为没有获取以前的位置,您可以设置位置侦听器并在getLastKnownLocation()
没有返回位置时获取当前的实时位置,按照在此链接中解释:***.com/a/17591377/8551764
你能根据那个做出改变吗,因为我仍然没有得到它。这将是一个很大的帮助以上是关于获取纬度和经度?的主要内容,如果未能解决你的问题,请参考以下文章