0

I'm using the master-flow detail template in android studio, and I keep getting a null pointer error when I try running my code when I call the following method in the onCreate() method of a Fragment class:

    private void setUpMapIfNeeded() {
    fm = getActivity().getSupportFragmentManager();
    if (mMap == null) {

        mMap = ((SupportMapFragment) fm.findFragmentById(R.id.mapFragment)).getMap();
    }
}

My layout is the following:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<fragment
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mapFragment"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_weight="0.9"
    tools:context=".CategoryDetailFragment"/>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="40dp">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/record_audio"
        android:id="@+id/recordaudiobutton"
        android:onClick="displayLongitudeLatitude"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/longitudeTester" />

</LinearLayout>

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mark.markmaptester2/com.example.mark.markmaptester2.CategoryDetailActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2367)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5323)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.mark.markmaptester2.CategoryDetailFragment.setUpMapIfNeeded(CategoryDetailFragment.java:177)
        at com.example.mark.markmaptester2.CategoryDetailFragment.onCreate(CategoryDetailFragment.java:72)
        at android.support.v4.app.Fragment.performCreate(Fragment.java:1766)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
        at android.app.Activity.performStart(Activity.java:5353)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340)

            

Perhaps it is a simple fix however I can not identify the problem Thanks

MichaelT
  • 15
  • 3

1 Answers1

0

Add this in your Manifest.xml

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="YourKey" />
    <activity
        android:name=".YourActivityClassName"
        android:label="Class Name" >
        <intent-filter>
            <action android:name="package.name.YOURACTIVITYNAME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</activity>

Check this: Maps Api Guide

Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54