简体   繁体   中英

Error inflating class fragment - sherlockactionbar google map v2

I am trying to develop a Google Android Map v2 inside a fragment with SherlockActivtiyBar.

I have this error:

04-12 10:01:26.830: E/AndroidRuntime(1089): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.it.michele.ab.map/it.michele.prova.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment

Can you help me?

What I am doing wrong?

Here my Eclipse project:

http://www.michelepierri.it/project.zip

This is my code:
SherlockMapFragment.java

package com.actionbarsherlock.app;

    import android.app.Activity;
    import android.support.v4.app.Watson.OnCreateOptionsMenuListener;
    import android.support.v4.app.Watson.OnOptionsItemSelectedListener;
    import android.support.v4.app.Watson.OnPrepareOptionsMenuListener;

    import com.actionbarsherlock.app.SherlockFragmentActivity;
    import com.actionbarsherlock.internal.view.menu.MenuItemWrapper;
    import com.actionbarsherlock.internal.view.menu.MenuWrapper;
    import com.actionbarsherlock.view.Menu;
    import com.actionbarsherlock.view.MenuInflater;
    import com.actionbarsherlock.view.MenuItem;
    import com.google.android.gms.maps.SupportMapFragment;

    public class SherlockMapFragment extends SupportMapFragment implements OnCreateOptionsMenuListener, OnPrepareOptionsMenuListener, OnOptionsItemSelectedListener {
        private SherlockFragmentActivity mActivity;

        public SherlockFragmentActivity getSherlockActivity() {
            return mActivity;
        }

        @Override
        public void onAttach(Activity activity) {
            if (!(activity instanceof SherlockFragmentActivity)) {
                throw new IllegalStateException(getClass().getSimpleName() + " must be attached to a SherlockFragmentActivity.");
            }
            mActivity = (SherlockFragmentActivity)activity;

            super.onAttach(activity);
        }

        @Override
        public void onDetach() {
            mActivity = null;
            super.onDetach();
        }

        @Override
        public final void onCreateOptionsMenu(android.view.Menu menu, android.view.MenuInflater inflater) {
            onCreateOptionsMenu(new MenuWrapper(menu), mActivity.getSupportMenuInflater());
        }

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            //Nothing to see here.
        }

        @Override
        public final void onPrepareOptionsMenu(android.view.Menu menu) {
            onPrepareOptionsMenu(new MenuWrapper(menu));
        }

        @Override
        public void onPrepareOptionsMenu(Menu menu) {
            //Nothing to see here.
        }

        @Override
        public final boolean onOptionsItemSelected(android.view.MenuItem item) {
            return onOptionsItemSelected(new MenuItemWrapper(item));
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            //Nothing to see here.
            return false;
        }
    }

MainActivity.java

   package it.michele.prova;

import com.actionbarsherlock.app.SherlockFragmentActivity;

import android.os.Bundle;
import android.app.Activity;
//import android.view.Menu;
import com.actionbarsherlock.view.*;

import com.example.it.michele.ab.map.R;
import com.example.it.michele.ab.map.R.layout;
import com.example.it.michele.ab.map.R.menu;
//import com.google.android.gms.maps.*;

public class MainActivity extends SherlockFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, (android.view.Menu) menu);
        return true;
    }

}

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fragment_map"
        android:name=".ui.fragments.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:tag="tag_fragment_map" />

    <fragment
        android:id="@+id/fragment_help"
        android:name=".ui.fragments.HelpFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:tag="tag_fragment_help" />
</FrameLayout>

在此处输入图片说明

Usualy if you receive the fragment/class not found error when working with maps then you problem is probably for lacking a good reference of the google-play-services library as MapFragment / SupportMapFragment are part of this library:

Check that yours Android Dependencies folder (blue) hold the google-play-services (orange) inside you will find the com.google.android.gms.maps package (green) that has the MapFragment class (red) :

在此处输入图片说明

If this is not the case you can check the first 3 steps of this blog post I wrote to get and idea on how to reference this package correctly:

Google Maps API V2

i have spent days trying to solve this. In my case, i didn't reference open GL es v2 in my android manifest - once i put that in there, the error went away and my map showed up. add the statement to your android manifest permissions if it isn't already in there - that may solve it for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM