我有一个使用操作栏选项卡的应用程序。我正在尝试升级应用程序以使用 Google Maps V2 (MapFragment),但是当 MapFragment 从主活动传递到片段容器时,它都崩溃了。
地图片段:
public class EstablishmentGoogleMapFragment extends MapFragment {
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = super.onCreateView(inflater, container, savedInstanceState);
mMap = getMap();
return root;
}
}
分片交易:
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(fragment == null){
String fragmentName = fragmentClass.getName();
fragment = Fragment.instantiate(activity, fragmentName);
ft.add(fragmentContainer, fragment, fragmentName);
} else {
ft.attach(fragment);
}
}
日志猫:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.rb.hellojson/com.rb.hellojson.EstablishmentGoogleMapFragment}:
java.lang.ClassCastException: com.rb.hellojson.EstablishmentGoogleMapFragment cannot be cast to android.app.Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
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:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: com.rb.hellojson.EstablishmentGoogleMapFragment cannot be cast to android.app.Activity
at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
... 11 more