一个片段中的Andr​​oid MapFragment [英] Android MapFragment within a Fragment

查看:246
本文介绍了一个片段中的Andr​​oid MapFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到相关的片段中加入地图碎片多个线程。但他们并没有帮我解决我的问题。我试着去地图片段添加到一个片段。但即时得到如下空指针异常:

I found many threads related to adding a map fragment within a fragment. But they didn't help me solve my problem. Im trying to add a map fragment to a fragment. But Im getting Null pointer exception as below :

11-25 10:19:55.222    9101-9101/com.uma.mobile E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.uma.mobile, PID: 9101
java.lang.NullPointerException
        at com.uma.mobile.subview.ShopDetails.<init>(ShopDetails.java:122)
        at com.uma.mobile.activity.Shop.onCreateView(Shop.java:119)
        at android.app.Fragment.performCreateView(Fragment.java:1700)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
        at android.app.BackStackRecord.run(BackStackRecord.java:684)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4998)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
        at dalvik.system.NativeStart.main(Native Method)

其中,Shop.java延伸片段和ShopDetails

延伸的RelativeLayout有mapFragement在里面。

where Shop.java extends Fragment and ShopDetails extends RelativeLayout which has mapFragement in it.

shop_details.xml:

<RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/mapTile"
            android:layout_marginTop="10dp"
            android:padding="10dp"
            android:background="@drawable/grey_gradient_rounded"
            android:layout_below="@+id/description">


            <RelativeLayout
                android:id="@+id/mapViewContainer"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/nearestLocation"
                android:layout_alignBottom="@+id/tapToView" />
                <!--<fragment xmlns:android="http://schemas.android.com/apk/res/android"-->
                    <!--android:id="@+id/mapView"-->
                    <!--android:layout_width="100dp"-->
                    <!--android:layout_height="wrap_content"-->
                    <!--android:name="com.google.android.gms.maps.MapFragment"/>-->

这是我如何添加地图片段

This is how I'm adding the map fragment

mapFragment = new MapFragment();
    FragmentTransaction transaction=  fm.findFragmentByTag("SHOPS").getChildFragmentManager().beginTransaction();
    transaction.add(R.id.mapViewContainer,mapFragment,"MAP_FRAG").commit();

这是我得到异常的地方:

This is the place where I get the exception:

try {

                    MapsInitializer.initialize(activity);
                    mapFragment.getMap().getUiSettings().setZoomControlsEnabled(false);
                    LatLng position = new LatLng(loc._latitude, loc._longitude);
                    MarkerOptions markerOptions = new MarkerOptions()
                            .position(position)
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow_pin2x));
                    mapFragment.getMap().addMarker(markerOptions);
                    mapFragment.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(position, 14));
                } catch (GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                    fm.beginTransaction().hide(mapFragment).commit();
                }
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow_pin2x));
                    map.addMarker(markerOptions);
                    map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 14));

mapFragment.getMap()。getUiSettings()

推荐答案

您应该使用图形页面中的片段,还支持地图V2。看到这个<一个href=\"https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapView\">https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapView

you should use mapview in fragment it also support map v2. see this https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapView

试试这个它会工作。

public class MyMapFragment extends Fragment {

    private MapView mMapView;
    private GoogleMap mMap;
    private Bundle mBundle;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflatedView = inflater.inflate(R.layout.map_fragment, container, false);

        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO handle this situation
        }

        mMapView = (MapView) inflatedView.findViewById(R.id.map);
        mMapView.onCreate(mBundle);
        setUpMapIfNeeded(inflatedView);

        return inflatedView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBundle = savedInstanceState;
    }

    private void setUpMapIfNeeded(View inflatedView) {
        if (mMap == null) {
            mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }
}

这是相应的RES /布局/ map_fragment.xml:

And here is the corresponding res/layout/map_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

您可以省略RelativeLayout的(和移动的xmlns declartion到新的根元素,在这种情况下com.google.android.gms.maps.MapView)如果你在你的布局只是一个单元,就像这个例子

You can omit the RelativeLayout (and move the xmlns declartion up to your new root element, in this case the com.google.android.gms.maps.MapView) if you have just one element in your layout like in this example.

这篇关于一个片段中的Andr​​oid MapFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆