Android - android.view.InflateException 错误 第二次转到 google 地图时

Android - android.view.InflateException error When going to google maps the second time

我在我的 android 应用程序中实现了一个 google 地图,它第一次运行良好。但是当我第二次尝试进入它时,应用程序崩溃了。

我发现我必须以编程方式声明 supportFragment 而无需在 xml 中进行操作。 但是堆栈溢出中的解决方案对我不起作用,因为我正在 class 中访问我的 google 映射,它扩展到 PagerAdapter 。它不支持 getChildFragmentManager()。我该如何克服这个问题?

我的class

    public class ViewPageAdapter extends PagerAdapter {

    private Fragment fragmentActivity;

    public ViewPageAdapter(ArrayList<BaseElement> item, int page,
                Activity activity, Fragment fragmentActivity) {
            super();
            this.item = item;
            this.page = page;
            this.activity = activity;
            this.fragmentActivity = fragmentActivity;
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            this.imageLoader = new ImageLoader(activity);
        }

    @Override
        public Object instantiateItem(ViewGroup container, int position) {

    View view = null;


if (page == Element.THEATER_DETAIL.getType()) {


    GoogleMap googleMap = null;

                SupportMapFragment fm = (SupportMapFragment) fragmentActivity
                        .getFragmentManager().findFragmentById(R.id.map);


                googleMap = fm.getMap();



                googleMap.addMarker(new MarkerOptions().position(
                        new LatLng(7.421226f, 80.401264f)).title("Hello world"));

                final LatLng SRILANKA = new LatLng(7.421226,80.401264);

                CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(SRILANKA)      // Sets the center of the map to SRILANKA
                .zoom(10)                   // Sets the zoom
                .bearing(360)                // Sets the orientation of the camera to east
                .tilt(30)                   // Sets the tilt of the camera to 30 degrees
                .build();                   // Creates a CameraPosition from the builder
                googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

            }

XML

<fragment 
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:name="com.google.android.gms.maps.SupportMapFragment"/> 

logcat

01-05 17:00:13.276: E/AndroidRuntime(16311): FATAL EXCEPTION: main
01-05 17:00:13.276: E/AndroidRuntime(16311): android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-05 17:00:13.276: E/AndroidRuntime(16311):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
01-05 17:00:13.276: E/AndroidRuntime(16311):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
01-05 17:00:13.276: E/AndroidRuntime(16311):    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
01-05 17:00:13.276: E/AndroidRuntime(16311):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-05 17:00:13.276: E/AndroidRuntime(16311):    at com.fortuna.cinemalk.adapter.ViewPageAdapter.instantiateItem(ViewPageAdapter.java:131)
01-05 17:00:13.276: E/AndroidRuntime(16311):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:829)
01-05 17:00:13.276: E/AndroidRuntime(16311):    at android.support.v4.view.ViewPager.populate(ViewPager.java:979)
01-05 17:00:13.276: E/AndroidRuntime(16311):    at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
01-05 17:00:13.276: E/AndroidRuntime(16311):    at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:440)

你添加视图了吗?你应该在 instantiateItem 函数中添加视图,

     LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.your_layout_name, null);

然后继续你的代码。