2

我正在 android 17 中创建一个应用程序,并且我正在尝试包含 Google Maps v2。

在我当前的设置中,我有一个带有两个选项卡的 ActionBar,一个指向旅行信息,一个指向 Google 地图。

我可以让谷歌地图显示在主要活动中,但我不能让地图显示在片段中。

这是我的地图类代码:

// Will hold the information for the "Maps" tab
public class Maps extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setCustomView(R.layout.rowlayout);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Venue Map

        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        RelativeLayout layoutView = (RelativeLayout)inflater.inflate(R.layout.action_bar_tab, null);
        TextView tabText = (TextView) layoutView.findViewById(R.id.tabText);
        ImageView tabImage = (ImageView) layoutView.findViewById(R.id.tabImage);

        String venueMapTab = "Travel Information";
        Tab tab = actionBar.newTab();
        TabListener<VenueMap> dayOne = new TabListener<VenueMap>(this,
                venueMapTab, VenueMap.class);
        tab.setTabListener(dayOne); 
        tabText.setText("Venue Map");
        tab.setCustomView(layoutView);
        actionBar.addTab(tab);

        // Google Maps

        RelativeLayout layoutViewMaps = (RelativeLayout)inflater.inflate(R.layout.action_bar_tab, null);
        tabText = (TextView) layoutViewMaps.findViewById(R.id.tabText);
        tabImage = (ImageView) layoutViewMaps.findViewById(R.id.tabImage);

        String googleMapsTab = "Google Maps";
        tab = actionBar.newTab();
        TabListener<GoogleMaps> dayTwo = new TabListener<GoogleMaps>(this,
                googleMapsTab, GoogleMaps.class);
        tab.setTabListener(dayTwo);
        tabText.setText("Google Maps");
        tab.setCustomView(layoutViewMaps);
        actionBar.addTab(tab);

    }

这是我的谷歌地图片段

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getActivity().setContentView(R.layout.google_maps);
}

还有 google_maps.xml:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.MapFragment"/>

还有我的日志猫错误:

01-28 13:44:09.830: E/AndroidRuntime(22253): FATAL EXCEPTION: main
01-28 13:44:09.830: E/AndroidRuntime(22253): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zshare.blog/com.zshare.blog.Maps}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.os.Looper.loop(Looper.java:137)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at java.lang.reflect.Method.invokeNative(Native Method)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at java.lang.reflect.Method.invoke(Method.java:511)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at dalvik.system.NativeStart.main(Native Method)
01-28 13:44:09.830: E/AndroidRuntime(22253): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.Activity.setContentView(Activity.java:1881)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at com.zshare.blog..VenueMap.onActivityCreated(VenueMap.java:32)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.Fragment.performActivityCreated(Fragment.java:1703)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.Activity.onCreateView(Activity.java:4736)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.Activity.setContentView(Activity.java:1881)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at com.zshare.blog.VenueMap.onActivityCreated(VenueMap.java:32)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.Fragment.performActivityCreated(Fragment.java:1703)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.Activity.performStart(Activity.java:5113)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
01-28 13:44:09.830: E/AndroidRuntime(22253):    ... 11 more
01-28 13:44:09.830: E/AndroidRuntime(22253): Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f05003b, tag null, or parent id 0x1020002 with another fragment for com.google.android.gms.maps.MapFragment
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.app.Activity.onCreateView(Activity.java:4722)
01-28 13:44:09.830: E/AndroidRuntime(22253):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
01-28 13:44:09.830: E/AndroidRuntime(22253):    ... 35 more

如果我在活动中显示地图,它可以工作,但在片段中我收到错误。任何解决此问题的帮助将不胜感激:)

4

1 回答 1

7

由于onActivityCreated()is a method on Fragment,看起来您正在尝试让片段加载片段,虽然可以使用 Android 支持库,但它不适用于您正在使用的本机片段。也不清楚你为什么要这样做。

您的“Google 地图片段”应该 com.google.android.gms.maps.MapFragment应该继承自 com.google.android.gms.maps.MapFragment,但它不应该是尝试加载 com.google.android.gms.maps.MapFragment的单独片段。

于 2013-01-28T14:22:40.007 回答