MapView里面的片段

时间:2017-05-30 20:04:23

标签: android android-fragments android-mapview

我有一个包含片段的TabLayout,一个片段包含一个MapView,但每次带有MapView的片段都从背景返回时,在片段尝试创建MapView时会抛出错误;

My Fragment看起来像这样:

public class DinerFoodMapFragment extends Fragment {

    private MapView mapView;
    private GoogleMap map;

    public DinerFoodMapFragment() {
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        super.onCreateView(inflater, container, savedInstanceState);

        View view = inflater.inflate(R.layout.fragment_diner_food_map, container, false);
        mapView = (MapView) view.findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState);
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }


        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {
                map = mMap;
                CameraUpdate center =
                        CameraUpdateFactory.newLatLng(new LatLng(25.6714,
                                -100.309));
                CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);

                map.moveCamera(center);
                map.animateCamera(zoom);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();

    }

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

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

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

此行崩溃mapView.onCreate(savedInstanceState);

这是错误:

05-30 14:53:27.725 20916-20916/com.frevo.frevo E/Parcel: Class not found when unmarshalling: android.support.v7.widget.RecyclerView$SavedState
                                                         java.lang.ClassNotFoundException: android.support.v7.widget.RecyclerView$SavedState
                                                             at java.lang.Class.classForName(Native Method)
                                                             at java.lang.Class.forName(Class.java:324)
                                                             at android.os.Parcel.readParcelableCreator(Parcel.java:2404)
                                                             at android.os.Parcel.readParcelable(Parcel.java:2358)
                                                             at android.os.Parcel.readValue(Parcel.java:2264)
                                                             at android.os.Parcel.readSparseArrayInternal(Parcel.java:2675)
                                                             at android.os.Parcel.readSparseArray(Parcel.java:1967)
                                                             at android.os.Parcel.readValue(Parcel.java:2321)
                                                             at android.os.Parcel.readArrayMapInternal(Parcel.java:2614)
                                                             at android.os.BaseBundle.unparcel(BaseBundle.java:221)
                                                             at android.os.Bundle.getBundle(Bundle.java:763)
                                                             at com.google.android.gms.maps.internal.ea.a(:com.google.android.gms.DynamiteModulesB:168)
                                                             at com.google.maps.api.android.lib6.impl.az.a(:com.google.android.gms.DynamiteModulesB:690)
                                                             at com.google.maps.api.android.lib6.impl.da.a(:com.google.android.gms.DynamiteModulesB:81)
                                                             at com.google.android.gms.maps.internal.z.onTransact(:com.google.android.gms.DynamiteModulesB:66)
                                                             at android.os.Binder.transact(Binder.java:387)
                                                             at com.google.android.gms.maps.internal.IMapViewDelegate$zza$zza.onCreate(Unknown Source)
                                                             at com.google.android.gms.maps.MapView$zza.onCreate(Unknown Source)
                                                             at com.google.android.gms.dynamic.zza$3.zzb(Unknown Source)
                                                             at com.google.android.gms.dynamic.zza$1.zza(Unknown Source)
                                                             at com.google.android.gms.maps.MapView$zzb.zzIL(Unknown Source)
                                                             at com.google.android.gms.maps.MapView$zzb.zza(Unknown Source)
                                                             at com.google.android.gms.dynamic.zza.zza(Unknown Source)
                                                             at com.google.android.gms.dynamic.zza.onCreate(Unknown Source)
                                                             at com.google.android.gms.maps.MapView.onCreate(Unknown Source)
                                                             at com.frevo.frevo.diner.fragments.food.DinerFoodMapFragment.onCreateView(DinerFoodMapFragment.java:83)
                                                             at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                             at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                             at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                             at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                             at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                             at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                             at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1984)
                                                             at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:626)
                                                             at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
                                                             at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
                                                             at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:668)
                                                             at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:630)
                                                             at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:611)
                                                             at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2191)
                                                             at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1164)
                                                             at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1157)
                                                             at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1127)
                                                             at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1426)
                                                             at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1536)
                                                             at android.view.View$PerformClick.run(View.java:21168)
                                                             at android.os.Handler.handleCallback(Handler.java:746)
                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                             at android.os.Looper.loop(Looper.java:148)
                                                             at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                             at java.lang.reflect.Method.invoke(Native Method)

我的xml看起来像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.frevo.frevo.diner.fragments.food.DinerFoodMapFragment">

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

    <com.yarolegovich.discretescrollview.DiscreteScrollView
        android:background="@drawable/shadow_gradient"
        android:id="@+id/discrete_scroll"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

错误说,您需要在build.gradle中使用recycleler视图依赖项或删除recyclerview。 Android正试图给您的视图充气,但您没有依赖。

要使用recyclerview,您需要额外的依赖项是支持库的一部分。

compile 'com.android.support:recyclerview-v7:25.0.0'

更多信息:

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

https://developer.android.com/training/material/lists-cards.html