Android Can';无法获取片段中的当前位置
我不知道为什么我在这段代码上得到空对象引用,这似乎很好,但当我按下按钮并调用connect方法时,它必须给我用户的当前位置,但它挂起并跳出,这是我的片段类的代码,我已经将权限添加到我的清单文件中,并且应用程序具有必要的权限 这不是一个重复的帖子我不知道为什么这里的人在重复的帖子中没有首先注意答案只是为了避免应用程序不挂起,即使在这里我们已经有访问位置的预授权,但它返回nullAndroid Can';无法获取片段中的当前位置,android,nullpointerexception,google-api-client,google-location-services,Android,Nullpointerexception,Google Api Client,Google Location Services,我不知道为什么我在这段代码上得到空对象引用,这似乎很好,但当我按下按钮并调用connect方法时,它必须给我用户的当前位置,但它挂起并跳出,这是我的片段类的代码,我已经将权限添加到我的清单文件中,并且应用程序具有必要的权限 这不是一个重复的帖子我不知道为什么这里的人在重复的帖子中没有首先注意答案只是为了避免应用程序不挂起,即使在这里我们已经有访问位置的预授权,但它返回null public class GalleryFragment extends Fragment implements Goo
public class GalleryFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
private EditText searchinput;
private Button searchbtn;
private JSONArray jsonArray;
private GoogleApiClient mLocationClient;
Location currentLocation;
@SuppressLint("ValidFragment")
public GalleryFragment(JSONArray array) {
// Required empty public constructor
this.jsonArray = array;
}
public GalleryFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_gallery, container, false);
if (mLocationClient == null) {
mLocationClient = new GoogleApiClient.Builder(this.getContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
searchinput = (EditText) rootView.findViewById(R.id.searchfld);
searchbtn = (Button) rootView.findViewById(R.id.searchbtn);
searchbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = searchinput.getText().toString();
mLocationClient.connect();
}
});
return rootView;
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.i("salam", " Connected");
if(ContextCompat.checkSelfPermission(this.getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
currentLocation = LocationServices.FusedLocationApi.getLastLocation(mLocationClient);
}
Log.i ("salam ", " " + currentLocation.getLongitude());
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
日志:
java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“float android.location.location.distanceTo(android.location.location)”
更新:我通过使用正确的访问请求方式修复了我的问题
然后:
最后通过此方法管理请求:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
boolean allowed = true;
switch (requestCode) {
case PERMISSION_ACCESS_FINE_LOCATION:
// If request is cancelled, the result arrays are empty.
for (int res : grantResults) {
allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
}
break;
default:
allowed = false;
break;
}
if (allowed) {
getLocation();
} else {
Toast.makeText(getContext(),"You need to 'Enable' the location service", Toast.LENGTH_SHORT).show();
}
}
祝你好运,谢谢
if(ContextCompat.checkSelfPermission(this.getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
currentLocation = LocationServices.FusedLocationApi.getLastLocation(mLocationClient);
}
Log.i ("salam ", " " + currentLocation.getLongitude());
这里您已经访问了currentLocation
外部if block
和if block
将在且仅当用户已授予相关权限时执行
您将获得NullPointerException
,因为用户未授予权限,并且未在此处分配currentLocation
如果用户尚未授予权限,您可以使用其他方式请求这些权限
ActivityCompat.requestPermissions(...)
并使用callback了解他是否已授予权限并访问您的位置数据
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
}
我也试过,它不起作用again@Rotwand为什么你把帖子复制了,而这不是答案,他们只是放了一个if语句来避免错误,它没有解决问题,位置仍然是空的,不应该是空的
我得到了空对象引用
然后,你得到了一个空点异常。因此,被骗者。没什么好惊讶的of@Rotwang所以因为所有的错误都有相同的原因?这是什么样的解释?我说我不能得到位置,它给我空的问题是“得到位置”为什么?因为空?为什么为空?=回答,因为所有错误都有相同的原因?
当然。所有的NPE都是由同一个问题引起的。你的问题应该是“为什么我从…那里得到一个空的回报”,因为这是你想知道的。您还应该描述您尝试过但没有解决问题的方法。
ActivityCompat.requestPermissions(...)
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
}