3

我已经处理这个问题一段时间了。我用这个问题作为参考: Using ActionBarSherlock With the New SupportMapFragment

我目前正在使用 ActionBarSherlock,并且正在使用 Vipul Shah 相当简单的地图实现。它工作得很好,完全符合我的需要。但是,在方向更改时,整个应用程序崩溃。这是我的日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.meetball_1/com.meetball_1.HomeActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
    at android.app.ActivityThread.access$700(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
    at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:133)
    at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:261)
    at com.meetball_1.HomeActivity.onCreate(HomeActivity.java:70)
    at android.app.Activity.performCreate(Activity.java:5133)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
    ... 12 more
    Caused by: java.lang.IllegalStateException: Fragment com.google.android.gms.maps.SupportMapFragment did not create a view.
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:303)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
    ... 24 more

我意识到无论出于何种原因都没有创建视图,并且错误来自应用程序未能扩展我的类片段。如何在不让事情变得太复杂的情况下解决这个问题,因为我觉得实现地图很简单,所以应该是我的解决方案。这是我的 xml 片段:

<fragment android:id="@+id/map"
    android:layout_width="fill_parent"
    android:layout_height="230dp"
    android:layout_weight="1"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_gravity="center_horizontal|top" />

这是我的相关代码片段:

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

 SupportMapFragment fragment = SupportMapFragment.newInstance();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.map, fragment).commit();
}

谢谢大家!

在这一点上,我已经尝试了一切。这是 Googlemap 的错误吗?我如何解决它?

4

2 回答 2

19

我有同样的问题,为了解决它,我不得不使用 FrameLayout 而不是片段:

<FrameLayout
        android:id="@+id/mapview"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

接着:

SupportMapFragment mMapFragment = (SupportMapFragment) getActivity()
            .getSupportFragmentManager().findFragmentById(R.id.mapview);

    if (mMapFragment == null) {
        mMapFragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.mapview, mMapFragment).commit();
    }
于 2013-10-03T18:55:17.110 回答
1

当布局包含片段时,您不能将布局膨胀为片段。仅当动态添加到片段时才支持嵌套片段。

以编程方式执行此操作。

于 2013-11-06T23:16:50.510 回答