2

尝试为手机创建 ListFragment --> MapFragment 活动。MapFragment 代码如下:

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));
    }
}

但我收到以下错误---

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()方法的时机有关吗?

4

1 回答 1

1

在这里改变了实现。使用了一个新布局,该布局在 FrameLayout 中同时具有 listFragment 和 MapFragment 的片段类。然后通过他们的 FragmentIds 找到这些,然后使用显示/隐藏与添加到后台堆栈以使地图/列表显示给用户。我不确定使用显示/隐藏的两组片段会产生什么开销,但似乎在 Nexus4 上工作得很好:)

此处提供代码: https ://github.com/codedawg82/AndroidFragments/

于 2013-02-16T13:43:42.010 回答