0

I have seen many other questions like this one on this website and tried changing my code to match but it never solves the problem.

My app loads with some buttons to push. One of them opens a class file that opens a map on the screen. The code for that class is :

public class FuelMap extends Activity 
{   

private final LatLng locBurb = new LatLng(49.27645, -122.917587);
private final LatLng locSura = new LatLng(49.187500, -122.849000);  
private GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fuel_map);

    //map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map )).getMap();
    // Show the Up button in the action bar.
    setupActionBar();
}

/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() 
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.fuel_map, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void OnClickCity(View C)
{
    //CameraUpdate update = CameraUpdateFactory.newLatLng(locBurb);
    //map.animateCamera(update);
}

public void OnClickBuraby(View B)
{

}

public void OnClickSurray(View S)
{

}

}

But if I uncomment the line: //map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map )).getMap();

and then the app will run until i try to switch it over to the map.

Like I said at the beginning of this I have searched and often found answers on this site relating to my question but none of them gave me any results. I do not believe this problem is due an error of installing the google map stuff needed for the android app since I can load a map. So anyone know what might be the cause?

-LogCat info

04-16 17:16:10.747: D/AndroidRuntime(11672): Shutting down VM
04-16 17:16:10.747: W/dalvikvm(11672): threadid=1: thread exiting with uncaught exception (group=0x41543300)
04-16 17:16:10.752: E/AndroidRuntime(11672): FATAL EXCEPTION: main
04-16 17:16:10.752: E/AndroidRuntime(11672): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testing.fuelapptesting/com.testing.fuelapptesting.FuelMap}: java.lang.NullPointerException
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.os.Looper.loop(Looper.java:137)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.ActivityThread.main(ActivityThread.java:4745)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at java.lang.reflect.Method.invoke(Method.java:511)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at dalvik.system.NativeStart.main(Native Method)
04-16 17:16:10.752: E/AndroidRuntime(11672): Caused by: java.lang.NullPointerException
04-16 17:16:10.752: E/AndroidRuntime(11672):    at com.testing.fuelapptesting.FuelMap.onCreate(FuelMap.java:31)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.Activity.performCreate(Activity.java:5008)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-16 17:16:10.752: E/AndroidRuntime(11672):    ... 11 more
04-16 17:16:13.580: I/Process(11672): Sending signal. PID: 11672 SIG: 9

-- activity_Fuel_Map layout

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/maps"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/busCity" />

    <TextView
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/maps"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="33dp"
        android:ems="10"
        android:text="Map" >

        <requestFocus />
    </TextView>

    <Button
        android:id="@+id/buttBuraby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/map"
        android:layout_toRightOf="@+id/buttCity"
        android:onClick="OnClickBuraby"
        android:text="Buraby" />

    <Button
        android:id="@+id/buttSurray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/map"
        android:layout_toRightOf="@+id/buttBuraby"
        android:onClick="OnClickSurray"
        android:text="Surray" />

    <Button
        android:id="@+id/buttCity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/buttBuraby"
        android:layout_alignBottom="@+id/buttBuraby"
        android:layout_alignLeft="@+id/maps"
        android:onClick="OnClickCity"
        android:text="City" />


</RelativeLayout>

-- Manifest file

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.testing.fuelapptesting"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <!-- The following two permissions are not required to use
        Google Maps Android API v2, but are recommended. -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>


    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.testing.fuelapptesting.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.testing.fuelapptesting.News"
            android:label="@string/title_activity_news"
            android:parentActivityName="com.testing.fuelapptesting.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.testing.fuelapptesting.MainActivity" />
        </activity>
        <activity
            android:name="com.testing.fuelapptesting.Messages"
            android:label="@string/title_activity_messages" >
        </activity>
        <activity
            android:name="com.testing.fuelapptesting.FuelMap"
            android:label="@string/title_activity_fuel_map"
            android:parentActivityName="com.testing.fuelapptesting.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.testing.fuelapptesting.MainActivity" />
        </activity>
        <activity
            android:name="com.testing.fuelapptesting.Scanner"
            android:label="@string/title_activity_scanner"
            android:parentActivityName="com.testing.fuelapptesting.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.testing.fuelapptesting.MainActivity" />
        </activity>
        <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />

        <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyC8hlQR9rdDGYMEXQCqpMD2PxzQRrpprPg"/>

    </application>

</manifest>
Ardel
  • 165
  • 3
  • 14

3 Answers3

1

try this link: in google maps V2 ... fragment.getMap() returns null

it appears that the map fragment you are trying to load hasn't been instantiated yet, in the onCreateView(). Hopefully this solves the issue. You might need to post to a handler after the map loads.. essentially a callback.

Community
  • 1
  • 1
reidisaki
  • 1,525
  • 16
  • 29
  • Still can't figure out what is wrong. I changed my code to work just like his in the onCreate method but my map still comes up null :( – Ardel Apr 17 '14 at 00:04
0

I t seems that you are using Maps V2 so your declaration in the xml is not right.

It is a fragment not a FragmentLayout as mention in the Android Developer

    <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>
Omar HossamEldin
  • 3,033
  • 1
  • 25
  • 51
  • Yea i did have that wrong thanks but the map is still coming up null :( – Ardel Apr 17 '14 at 00:28
  • The same **logcat** log and the same error ? if yes, did you read this tutorial ,You sure you used the library in the `AndroidManifest.xml` ?, what is the Android version of the device you run this application on? – Omar HossamEldin Apr 17 '14 at 00:37
  • Yes like I said in the question I can get the map to load if i comment out the line that im having trouble with but once I add it i have issues. After I followed that tutorial to install everything I started to follow this tutorial https://www.youtube.com/watch?v=awX5T-EwLPc and I stopped when I could not get the map to show up – Ardel Apr 17 '14 at 00:41
0

Thanks a ton for all the help guys. I final figured out the problem. It was the letter 's'.

In this line: //map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map )).getMap(); I was was pulling the object called map. If you look in my activity_Fuel_Map layout the map object is a TextView.

`    <TextView
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/maps"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="33dp"
        android:ems="10"
        android:text="Map" >`

So I had to change the (R.id.map) to (R.id.maps) to pull my map fragment.

<fragment
    android:id="@+id/maps"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/busCity" />

Thanks again for all the help guys.

Ardel
  • 165
  • 3
  • 14