是否可以在ActionBar选项卡中放置Google Maps V2 MapFragment?

时间:2013-04-05 18:28:16

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

我有一个使用操作栏标签的应用程序。我正在尝试升级应用程序以使用Google Maps V2(MapFragment),但是当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);
        }
    }

logcat的:

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

1 个答案:

答案 0 :(得分:1)

这个错误应该是明确的:

Caused by: java.lang.ClassCastException: com.rb.hellojson.EstablishmentGoogleMapFragment cannot be cast to android.app.Activity

您是尝试对此片段执行startActivity还是什么?

对于以后的修复:mMap = getMap();此时将返回null(或者可能不是)。

编辑:

某些东西正在开始你的片段,就像它是一项活动。如果它不是Launcher,因为片段是在Manifest中定义的......