我一直在处理这个问题一段时间了。我用这个问题作为参考: Using ActionBarSherlock With the New SupportMapFragment
我目前正在使用ActionBarSherlock,我正在使用Vipul Shah相当简单的地图实现。它的工作非常出色,完全符合我的需要。但是,在Orientation更改时,整个应用程序崩溃。这是我的日志:
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的错误吗?我该如何解决?
答案 0 :(得分:17)
我有同样的问题并解决它,我不得不使用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();
}
答案 1 :(得分:1)
当布局包含片段时,您无法将布局扩展为片段。只有动态添加到片段时才支持嵌套片段。
以编程方式执行此操作。