Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SupportMapFragment can't see map inside a fragment

I'm trying to add a SupportMapFragment into a FrameLayout in my code but i get a Null Pointer Exeption.

My Code:

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

My 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>

My 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
like image 857
Asaf Nevo Avatar asked Dec 04 '22 00:12

Asaf Nevo


1 Answers

Your simply forgetting to call super.onCreateView(); in your SecondFragment.

If you override onCreateView() of SupportMapFragment you need to remember to call the super method as this is where all the "action" happens.

like image 199
Warpzit Avatar answered Dec 23 '22 05:12

Warpzit