【发布时间】:2014-03-24 19:39:08
【问题描述】:
我一直在尝试让位置管理器在我的 Fragment 中工作几个小时。 我发现了一个关于类似问题的 StackOverflow 问题,并尝试实施该解决方案。 答案在这里:https://stackoverflow.com/a/18533440/3035598
所以我几乎从字面上复制了答案所说的所有内容,但这对我不起作用。 当地图打开时,我收到错误“Google Play Services Missing”。这是由 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() ,它们都不是空的,它们返回一个正确的值。
【问题讨论】:
-
检查您的设备中是否安装了 google play 服务。
-
在我的设备中?我正在使用 Nexus 5 (Android 4.4)。我该怎么做?
-
您是否在项目中添加了 Google play services 作为库?
-
@Naddy,是的,我有。我已将其作为库包含并在我的项目属性中将其设置为库。
-
""Google Play 服务缺失",因为未安装 Google Play services。
标签: java android android-fragments locationmanager