Android SupportMapFragment无法在片段中看到地图

时间:2013-03-07 19:24:08

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

我正在尝试在我的代码中将一个SupportMapFragment添加到FrameLayout中,但我得到一个Null Pointer Exeption。

我的代码:

MainFragment:

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();
    }       
}

SecondFragment:

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

2 个答案:

答案 0 :(得分:18)

您只是忘记在super.onCreateView();中致电SecondFragment

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

答案 1 :(得分:0)

我也有同样的问题,所以我的临时解决方案是不使用SupportMapFragment来改变使用MapFragment。

希望这可以帮到你。