java - 无法让位置管理器在 Fragment 内工作

标签 java android android-fragments locationmanager

我一直在尝试让位置管理器在我的 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/

相关文章:

android - 如何使用文本+图像动态填充 Android 微调器

安卓 GTM : Possible to start new Google Analytics session manually?

android - Android 中的实时音频录制和播放以及线程和回调处理

java - 用于匹配扑克牌的正则表达式 (Java)

java - 如何防止在模板中使用环境变量?

java - MyContentProvider.onCreate() 没有被调用?

android - 禁用 ActionBarDrawerToggle 抽屉指示器,但保留汉堡包图标

android - 无法显示已添加的 fragment

android - fragment 问题

java - 如何使用 Selenium 测试通过 Web 应用程序的注册页面输入的数据是否存储到数据库中?