当我在片段中对GoogleMaps进行充气时,我会得到error。
错误行是MapsFragment
:
View rootView = inflater.inflate(R.layout.map_fragment,container, false);
MainActivity:
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);
}
}
MapsFragment:
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" />
答案 0 :(得分: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" />