Android SupportMapFragment не может видеть карту внутри фрагмента

Я пытаюсь добавить 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.zronResume(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.ea(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(); вызвать super.onCreateView(); В вашем SecondFragment .

Если вы переопределите onCreateView() SupportMapFragment, вам нужно помнить, что вы вызываете супер-метод, так как здесь происходит все «действие».

У меня тоже такая же проблема, поэтому мое временное решение не использует SupportMapFragment для изменения с помощью MapFragment.

Надеюсь, это может вам помочь.