我有一个带有横向栏(如菜单栏)的 FragmentActivity,以及一个用于将片段放在那里的容器。onClickListener
从这个 FragmentActivity 我使用每个按钮(从侧栏)调用其他片段。
例如,调用 FragmentActivity MenuViewActivity
。这会调用Navigation
片段并在容器上设置它的布局。
在Navigation
片段中,我有一个启动按钮GoogleMaps
(或者这就是我需要得到的)
[MenuViewActivity(FragmentActivity)] >> [NavigationFragment(Fragment)] >> [GoogleMapsFragment(Fragment) ]
这个想法是将 GoogleMaps 窗口安装在我设置其他片段(如 navigationFragment)的同一个容器中
这是NavigationFragment.java的代码:
public class NavigationFragment extends Fragment implements OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**Inflate the layout for this fragment*/
View mainView = inflater.inflate(R.layout.navegacion, container, false);
mapBtn = (ImageView) mainView.findViewById(R.id.mapsButton);
navBtn = (ImageView) mainView.findViewById(R.id.navigatorButton);
mapBtn.setOnClickListener(this);
navBtn.setOnClickListener(this);
return mainView;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.mapsButton:
GoogleMapsFragment fragment_maps = new GoogleMapsFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, fragment_maps).commit();
break;
}
}
这是GoogleMapsFragment.java的代码:
public class GoogleMapsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**Inflate the layout for this fragment*/
View mainView = inflater.inflate(R.layout.google_maps, container, false);
SupportMapFragment fm = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
mMap = fm.getMap();
return mainView;
}
以及google_maps.xml的代码:
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
map:cameraTilt="45"
map:cameraZoom="14"
class="com.google.android.gms.maps.SupportMapFragment" />
我认为我没有在onClick上正确调用GoogleMapsFragment,或者我不能像在NavigationFragment中那样从其他片段调用 *fragment_container* ,因为这个 id 属于MenuViewActivity的布局。
logCat 输出是这样的:
android.view.InflateException: Binary XML file line #3: Error inflating class fragment
at com.example.GoogleMapsFragment.onCreateView(GoogleMapsFragment.java:47)
Caused by: java.lang.IllegalStateException: A required meta-data tag in your app's
AndroidManifest.xml does not exist. You must have the following declaration within the <application> element:
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />