我正在尝试将 SupportMapFragment 添加到我的代码中的 FrameLayout 中,但我得到了一个空指针异常(exception)。

我的代码:

主 fragment :

public class MainFragment extends FragmentActivity{

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_main);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft = getSupportFragmentManager().beginTransaction();
        SecondFragment second = new SecondFragment();
        ft.add(R.main_activity.frame, second);
        ft.addToBackStack(null);
        ft.commit();
    }
}

第二个 fragment :
public class SecondFragment extends SupportMapFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_map, null);
    }
}

我的 XML:

activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <include
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        layout="@layout/header" />

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="7.06"
        android:id="@+main_activity/frame"
        android:background="@drawable/defaultbg"/>

    <include
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        layout="@layout/footer" />

</LinearLayout>

Activity map :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</FrameLayout>

我的 Logcat:
03-07 21:20:17.160: E/AndroidRuntime(1617): FATAL EXCEPTION: main
03-07 21:20:17.160: E/AndroidRuntime(1617): java.lang.RuntimeException: Unable to resume activity {com.WhosAround/com.WhosAround.MainFragment}: java.lang.NullPointerException
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.os.Looper.loop(Looper.java:130)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.app.ActivityThread.main(ActivityThread.java:3691)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at java.lang.reflect.Method.invokeNative(Native Method)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at java.lang.reflect.Method.invoke(Method.java:507)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at dalvik.system.NativeStart.main(Native Method)
03-07 21:20:17.160: E/AndroidRuntime(1617): Caused by: java.lang.NullPointerException
03-07 21:20:17.160: E/AndroidRuntime(1617):     at maps.z.r.onResume(Unknown Source)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at com.google.android.gms.maps.internal.IMapFragmentDelegate$Stub.onTransact(IMapFragmentDelegate.java:115)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.os.Binder.transact(Binder.java:279)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onResume(Unknown Source)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at com.google.android.gms.maps.SupportMapFragment$a.onResume(Unknown Source)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at com.google.android.gms.internal.e$1.a(Unknown Source)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at com.google.android.gms.internal.e.a(Unknown Source)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at com.google.android.gms.internal.e.onResume(Unknown Source)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at com.google.android.gms.maps.SupportMapFragment.onResume(Unknown Source)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.support.v4.app.Fragment.performResume(Fragment.java:1503)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1871)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:455)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:444)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.app.Activity.performResume(Activity.java:3867)
03-07 21:20:17.160: E/AndroidRuntime(1617):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114)
03-07 21:20:17.160: E/AndroidRuntime(1617):     ... 12 more

最佳答案

您只是忘记在 super.onCreateView(); 中调用 SecondFragment

如果您覆盖 SupportMapFragment 的 onCreateView(),您需要记住调用 super 方法,因为这是所有“操作”发生的地方。

关于Android SupportMapFragment 无法看到 fragment 内的 map ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15279953/

10-10 08:29