错误:无法将MapFragment强制转换为android.support.v4.app.Fragment

时间:2013-08-14 09:11:14

标签: android google-maps-android-api-2

首先,我在这里观看:Start FragmentActivity from Activity现在我遇到了以下问题:

MapsActivity:

public class MapsActivity extends FragmentActivity {

private GoogleMap mMap;

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

并希望从 MainActivity 开始:

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

该活动已在Android Manifest中注册:

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

错误

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

有任何建议如何解决?

到目前为止感谢:)

1 个答案:

答案 0 :(得分:142)

正在扩展FragmentActivity,表示您正在使用与较低版本的android兼容的支持库v4。将MapFragment替换为xml文件中的SupportMapFragmentSupportMapFragment是与Android支持包一起使用的版本。 MapFragment用于本机API Level 11版本的片段。