Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

android - Having an issue with GoogleMap v2 crashing app on orientation change

I've been dealing with this problem for a while now. I used this question as a reference: Using ActionBarSherlock With the New SupportMapFragment

I'm currently using ActionBarSherlock and I'm using Vipul Shah's rather simple implementation of a map. It works brilliantly, does exactly what I need it to. However, on Orientation change, the entire app crashes. Here is my log:

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

I realize that for whatever reason a view is not being created and that the error comes from the app failing to inflate my class fragment. How do I fix this without making things too complicated because I feel that implementing the map was simple so should be my solution. here is my xml snippet:

<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" />

Here is my relevant code snippet:

@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();
}

Thank you all in advance!

I've tried everything at this point. Is this a bug with Googlemap? How do I fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the same problem and to solve it, I had to use FrameLayout instead of fragment:

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

And then:

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

    if (mMapFragment == null) {
        mMapFragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.mapview, mMapFragment).commit();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...