我一直在尝试让位置管理器在我的 fragment 中工作几个小时。 我发现了一个关于类似问题的 StackOverflow 问题,并尝试实现该解决方案。 答案在这里:https://stackoverflow.com/a/18533440/3035598
所以我几乎复制了答案所说的所有内容,但它对我不起作用。 本地图打开时,我收到错误“Google Play 服务缺失”。这是由 NullPointerException 引起的,您可以在答案中看到。
我不知道为什么它不起作用,因为我按照他说的做了。
有人知道出了什么问题吗?
如果我必须提供我的代码,请告诉我,我会这样做,但它与我提供的链接中的几乎相同。
<小时/>编辑:
我使用的代码:
package com.example.bt6_aedapp;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;
public class fragmentB extends Fragment implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener {
private GoogleMap map;
private LatLng latlng;
private LocationRequest lr;
private LocationClient lc;
MapFragment mapFragment;
ImageView iv;
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if(parent != null) {
parent.removeView(view);
}
}
try {
view = inflater.inflate(R.layout.fragment_b, container, false);
mapFragment = ((MapFragment) this.getActivity().getFragmentManager().findFragmentById(R.id.map));
iv = (ImageView) view.findViewById(R.id.iv);
map = mapFragment.getMap();
map.getUiSettings().setAllGesturesEnabled(false);
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
map.getUiSettings().setZoomControlsEnabled(false);
MapsInitializer.initialize(this.getActivity());
}
catch (InflateException e) {
Toast.makeText(getActivity(), "Problems inflating the view !", Toast.LENGTH_LONG).show();
}
catch (NullPointerException e) {
Toast.makeText(getActivity(), "Google Play Services missing !", Toast.LENGTH_LONG).show();
}
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lr = LocationRequest.create();
lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
lc = new LocationClient(this.getActivity().getApplicationContext(),
this, this);
lc.connect();
}
@Override
public void onLocationChanged(Location location) {
latlng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latlng, 10);
map.animateCamera(cameraUpdate);
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
}
@Override
public void onConnected(Bundle connectionHint) {
lc.requestLocationUpdates(lr, this);
}
@Override
public void onDisconnected() {
}
}
我现在遇到的错误位于第 115 行: java.lang.NullPointerException 在 com.example.bt6_aedapp.fragmentB.onLocationChanged(fragmentB.java:155)
我检查了location.getLatitude()和location.getLongitude(),它们都不是空的,它们返回了正确的值。
最佳答案
好吧,经过大量的调试和研究,我找到了解决方案。
我所要做的就是更换
`mapFragment = ((MapFragment) this.getActivity().getFragmentManager().findFragmentById(R.id.map));`
与:
mapFragment = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map));
关于java - 无法让位置管理器在 Fragment 内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22606654/