无法获取位置管理器里面的工作片段 [英] Can't get Location Manager to work inside Fragment
问题描述
我一直在试图让一个位置管理到几个小时的工作,现在我的片段中。
我发现有关类似问题的出现StackOverflow的问题,并试图实施该解决方案。
答案是位于:<一href=\"http://stackoverflow.com/a/18533440/3035598\">http://stackoverflow.com/a/18533440/3035598
I've been trying to get a Location Manager to work for a few hours now inside my Fragment. I found a StackOverflow question about a similar problem, and tried to implement the solution. The answer is located here: http://stackoverflow.com/a/18533440/3035598
所以,我几乎逐字复制一切的答案说,但它不是为我工作。
当打开地图我得到的错误谷歌Play服务。这是因为你可以在复读NullPointerException异常引起的。
So i almost literally copied everything the answer said, but it is not working for me. When the map opens I get the error "Google Play Services Missing". This is caused by a NullPointerException as you can read in the answer.
我不知道为什么它不工作,因为我所做的一切,他说。
I have no idea why it is not working, since I did everything he said.
有谁知道是怎么回事了?
Does anyone know what's going wrong?
如果我必须提供我的code,让我知道,我会做到这一点,但它几乎等同于我所提供的链接。
If I have to provide my code, let me know and I will do that, but it is almost the same as in the link I provided.
编辑:
在code我使用:
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)
The error I'm getting now is located at row 115: java.lang.NullPointerException at com.example.bt6_aedapp.fragmentB.onLocationChanged(fragmentB.java:155)
我查了location.getLatitude()和location.getLongitude()和两者都是不为空,它们返回正确的值。
I checked the location.getLatitude() and location.getLongitude() and both of them are NOT empty, they return a correct value.
推荐答案
好吧,所以很多的调试和研究之后,我已经找到了解决方案。
Alright, so after a lot of debugging and research, I've found the solution.
我所要做的就是替换
`mapFragment = ((MapFragment) this.getActivity().getFragmentManager().findFragmentById(R.id.map));`
与
mapFragment =((SupportMapFragment)getFragmentManager()findFragmentById(R.id.map));
这篇关于无法获取位置管理器里面的工作片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!