0

当我在片段中膨胀 GoogleMaps 时,我得到了这个错误。错误行是这样的MapsFragment

View rootView = inflater.inflate(R.layout.map_fragment,container, false);

主要活动:

public class MainActivity extends Activity {
    public static Context appContext;

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

        // ActionBar gets initiated
        ActionBar actionbar = getActionBar();
        // Tell the ActionBar we want to use Tabs.
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        // initiating both tabs and set text to it.
        ActionBar.Tab aroundTab = actionbar.newTab().setText(getString(R.string.title_around));
        ActionBar.Tab mapTab = actionbar.newTab().setText(getString(R.string.title_map));
        ActionBar.Tab favoriteTab = actionbar.newTab().setText(getString(R.string.title_favorite));
        ActionBar.Tab achiveTab = actionbar.newTab().setText(getString(R.string.title_achievement));

        // create the two fragments we want to use for display content
         Fragment map = new MapsFragment();
         Fragment around = new AroundFragment();
         Fragment favorite = new FavoriteFragment();
         Fragment achive = new AchievementFragment();

        // Fragment StationsFragment = new BFragment();

        // set the Tab listener. Now we can listen for clicks.
         aroundTab.setTabListener(new MyTabsListener(map));
         mapTab.setTabListener(new MyTabsListener(around));
         favoriteTab.setTabListener(new MyTabsListener(favorite));
         achiveTab.setTabListener(new MyTabsListener(achive));

        // add the two tabs to the actionbar
        actionbar.addTab(aroundTab);
        actionbar.addTab(mapTab);
        actionbar.addTab(favoriteTab);
        actionbar.addTab(achiveTab);
    }
}

地图片段:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.map_fragment,container, false);
    map = ((MapFragment)getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
    map.setMyLocationEnabled(true);
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    mMapFragment = MapFragment.newInstance();
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.map,mMapFragment);
    fragmentTransaction.commit();
    return rootView;
}

显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jertt.yummymap"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />
    <uses-library android:name="com.google.android.maps" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <!-- The following two permissions are not required to use
         Google Maps Android API v2, but are recommended. -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.jertt.yummymap.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIza******alp2M"/>
    </application>

</manifest>

map_fragment.xml

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />
4

1 回答 1

0

android:name在类的布局文件中指定属性MapsFragment

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.jertt.yummymap.MapsFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
于 2013-10-11T10:12:46.847 回答