1

我有一个使用操作栏选项卡的应用程序。我正在尝试升级应用程序以使用 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
4

1 回答 1

1

这个错误应该很清楚:

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

你是想在这个片段上做 startActivity 还是什么?

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

编辑:

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

于 2013-04-05T18:45:01.650 回答