Error: MapFragment cannot be cast to android.suppo

2019-01-11 08:59发布

First, I watched out here: Start FragmentActivity from Activity and now I have the following problem:

MapsActivity:

public class MapsActivity extends FragmentActivity {

private GoogleMap mMap;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maps);
    setUpMapIfNeeded();
}
...

and want to start it out of the MainActivity with:

startActivity(new Intent(this, MapsActivity.class));

The activity is registered in Android Manifest:

<activity android:name="de.xbjoernx.gapp.MapsActivity"></activity>

Error

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.xbjoernx.gapp/de.xbjoernx.gapp.MapsActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
        at android.app.ActivityThread.access$600(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5227)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
        at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:323)
        at android.app.Activity.setContentView(Activity.java:1881)
        at de.xbjoernx.gapp.MapsActivity.onCreate(MapsActivity.java:19)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
        ... 11 more
Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
        at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
        at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
        ... 20 more

Any suggestions how to fix it?

Thanks so far :)

1条回答
倾城 Initia
2楼-- · 2019-01-11 09:27

as you are extending FragmentActivity which indicates you are using Support library v4 compatible with lower version of android. Replace MapFragment with SupportMapFragment inside your xml file. SupportMapFragment is the one to use with the Android Support package. MapFragment is for the native API Level 11 version of fragments.

查看更多
登录 后发表回答