Android - android.view.InflateException错误第二次去谷歌地图时

时间:2015-01-05 11:31:10

标签: android google-maps supportmapfragment

我已经在我的Android应用程序中实现了谷歌地图它第一次正常工作。但是当我第二次尝试进入它时,应用程序崩溃了。

我发现我必须以编程方式声明supportFragment而不在xml中使用它。 但是堆栈溢出的解决方案对我来说不起作用,因为我在一个扩展为 PagerAdapter 的类中访问我的谷歌地图。它不支持getChildFragmentManager()。我如何克服这个问题?

我的班级

    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)

1 个答案:

答案 0 :(得分:0)

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

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

然后继续您的代码。