本文介绍了MapFragment错误 - CameraUpdateFactory未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图建立一个ListFragment - >的电话MapFragment活动。 MapFragment code如下:

Trying to create a ListFragment --> MapFragment activity for the phone. MapFragment code follows:

public class MapFragment extends SupportMapFragment {
    private GoogleMap mMap;

    @Override
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);

        try {
            MapsInitializer.initialize(this.getActivity());
        } catch (GooglePlayServicesNotAvailableException e){
            e.printStackTrace();
        }

        setRetainInstance(true);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        if(getMap() != null){
            Log.v(TAG, "Map ready for use!");
            mMap = getMap();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = super.onCreateView(inflater, container, savedInstanceState);
        return root;
    }

    public void animateTo(double lat, double lng){
            Log.d(TAG, "mMap animating...");
            LatLng mCurrentPosition = new LatLng(lat, lng);
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mCurrentPosition, 16));
    }
}

不过,我得到以下错误---

But I get the following error ---

02-15 17:12:53.232: E/AndroidRuntime(6913): FATAL EXCEPTION: main
02-15 17:12:53.232: E/AndroidRuntime(6913): java.lang.NullPointerException: CameraUpdateFactory is not initialized
02-15 17:12:53.232: E/AndroidRuntime(6913):     at com.google.android.gms.internal.at.a(Unknown Source)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at com.google.android.gms.maps.CameraUpdateFactory.J(Unknown Source)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at com.google.android.gms.maps.CameraUpdateFactory.newLatLngZoom(Unknown Source)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at com.test.app.MapFragment.animateTo(MapFragment.java:113)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at com.test.app.MapActivity.onListItemClick(MapActivity.java:270)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at com.test.app.SimpleListFragment.onListItemClick(SimpleListFragment.java:65)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at android.widget.AbsListView$1.run(AbsListView.java:3423)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at android.os.Handler.handleCallback(Handler.java:725)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at android.os.Looper.loop(Looper.java:137)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at java.lang.reflect.Method.invokeNative(Native Method)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at java.lang.reflect.Method.invoke(Method.java:511)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-15 17:12:53.232: E/AndroidRuntime(6913):     at dalvik.system.NativeStart.main(Native Method)

我想调用MapInitializer将修复它,但似乎并没有这样的伎俩。这是否有些事情要与调用的GetMap()法的时机?

推荐答案

在这里改变了落实。采用了全新的布局有listFragment两个片段类和一个的FrameLayout内MapFragment。随后发现这些可以通过FragmentIds,然后用于显示/隐藏结合加入到背面叠层以使地图/列表显示给用户。我不知道这个开销招致使用两套片段被显示/隐藏,但似乎在Nexus4很好地工作:)

Changed up the implementation here. Used a new layout that had both fragment classes of the listFragment and the MapFragment within a FrameLayout. Then found these by their FragmentIds and then used show/hide in conjunction with adding to the back stack to make the map/list show to the user. I'm not sure what overhead this incurs using two sets of fragments being shown/hidden, but seems to work quite nicely on a Nexus4 :)

code可在这里:

Code available here:https://github.com/codedawg82/AndroidFragments/

这篇关于MapFragment错误 - CameraUpdateFactory未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 00:11