2

Good afternoon everyone,

I've had multiple issues with setting up everything before getting started with Android development that now have been resolved.

However after having followed google's tutorial on how to set up google maps on an Android device I'm slowly starting to give up...

I went through the entirety of the Creating a Map -> Map Objects until I started getting nullpointer exceptions in regards to my getMapAsync(this) and my fragmentManager so I decided to go for the googlemap approach instead.

However even though I am able to display my map fine, I am now unable to show markers on the map.

I'd love for someone to have a quick look in my code to see if they can find any problems. I've already looked through SO and found multiple questions regarding this subject, but none that actually has helped me.

Here is my class:

package com.example.birdview;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class Startup extends FragmentActivity 
implements OnMapReadyCallback {

    /*
     * Private methods:
     * 
     */
    private void setUpMap(){
        try {
             if (googleMap == null) {
                googleMap = ((MapFragment) getFragmentManager().
                findFragmentById(R.id.map)).getMap();
             }
             googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

          }
          catch (Exception e) {
             e.printStackTrace();
          }
    }

    /*
     * Private variables:
     */

    double latitude = 13.094192 ;
    double longitude = 80.121689;
    private GoogleMap googleMap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startup);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }
        setUpMap();

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_startup, container, false);
            return rootView;
        }
    }

    @Override
    public void onMapReady(GoogleMap map) {
        Marker hamburg = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude))
                  .title("Hello Maps"));
        // TODO Auto-generated method stub

    }


}

fragment_startup.xml:

<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="com.example.birdview.Startup$PlaceholderFragment" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />


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

activity_startup:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.birdview.Startup"
tools:ignore="MergeRootFrame" />

birdview_manifest:

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />
    <!--  Set application permissions: -->
    <permission
      android:name="com.example.birdview.permission.MAPS_RECEIVE"
      android:protectionLevel="signature" />

        <uses-permission android:name="your.package.name.permission.MAPS_RECEIVE"/> 
        <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" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


   <!--  Set openGL permissions: -->
   <uses-feature
      android:glEsVersion="0x00020000"
      android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <!--  meta-data for google maps api credentials: -->
        <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyAbIbXmufKtMn6t72Ya4oipOK1iowIEPYY"/>

        <meta-data 
             android:name="com.google.android.gms.version"
             android:value="@integer/google_play_services_version" />


        <activity
            android:name=".Startup"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

As mentioned I first was getting nullpointerexception when using the implementation that google (google maps api) provides. After having changed to the current implementation I am now able to show the map, with no instances of markers...

Here is the logcat:

02-15 12:56:12.240: D/ViewRootImpl(17859): ViewPostImeInputStage ACTION_DOWN
02-15 12:56:14.262: V/ActivityThread(17859): updateVisibility : ActivityRecord{4a02b89 token=android.os.BinderProxy@35c8e20e {com.example.birdview/com.example.birdview.Startup}} show : false
02-15 12:56:21.619: D/ResourcesManager(20806): creating new AssetManager and set to /data/app/com.example.birdview-1/base.apk
02-15 12:56:21.709: W/ResourceType(20806): For resource 0x7f02007c, entry index(124) is beyond type entryCount(56)
02-15 12:56:21.709: W/ResourceType(20806): Failure getting entry for 0x7f02007c (t=1 e=124) (error -75)
02-15 12:56:21.719: E/Resources(20806): RunTimeException
02-15 12:56:21.719: E/Resources(20806): android.content.res.Resources$NotFoundException: Resource ID #0x7f02007c
02-15 12:56:21.719: E/Resources(20806):     at android.content.res.Resources.getValue(Resources.java:2345)
02-15 12:56:21.719: E/Resources(20806):     at android.content.res.Resources.startRC(Resources.java:1059)
02-15 12:56:21.719: E/Resources(20806):     at android.app.ActivityThread$mRunnable.run(ActivityThread.java:2508)
02-15 12:56:21.719: E/Resources(20806):     at java.lang.Thread.run(Thread.java:818)
02-15 12:56:21.759: W/System.err(20806): java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.MapFragment.getMap()' on a null object reference
02-15 12:56:21.759: W/System.err(20806):    at com.example.birdview.Startup.setUpMap(Startup.java:31)
02-15 12:56:21.759: W/System.err(20806):    at com.example.birdview.Startup.onCreate(Startup.java:57)
02-15 12:56:21.759: W/System.err(20806):    at android.app.Activity.performCreate(Activity.java:6289)
02-15 12:56:21.759: W/System.err(20806):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
02-15 12:56:21.759: W/System.err(20806):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
02-15 12:56:21.759: W/System.err(20806):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
02-15 12:56:21.759: W/System.err(20806):    at android.app.ActivityThread.access$900(ActivityThread.java:177)
02-15 12:56:21.759: W/System.err(20806):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
02-15 12:56:21.759: W/System.err(20806):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-15 12:56:21.759: W/System.err(20806):    at android.os.Looper.loop(Looper.java:145)
02-15 12:56:21.759: W/System.err(20806):    at android.app.ActivityThread.main(ActivityThread.java:5942)
02-15 12:56:21.759: W/System.err(20806):    at java.lang.reflect.Method.invoke(Native Method)
02-15 12:56:21.759: W/System.err(20806):    at java.lang.reflect.Method.invoke(Method.java:372)
02-15 12:56:21.759: W/System.err(20806):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
02-15 12:56:21.759: W/System.err(20806):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
02-15 12:56:21.759: D/Activity(20806): performCreate Call secproduct feature valuefalse
02-15 12:56:21.759: D/Activity(20806): performCreate Call debug elastic valuetrue
02-15 12:56:21.769: I/zzad(20806): Making Creator dynamically
02-15 12:56:21.769: D/ResourcesManager(20806): creating new AssetManager and set to /data/app/com.google.android.gms-2/base.apk
02-15 12:56:21.780: W/ResourcesManager(20806): Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
02-15 12:56:21.780: W/ResourcesManager(20806): Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
02-15 12:56:21.910: D/ChimeraCfgMgr(20806): Reading stored module config
02-15 12:56:21.940: D/ChimeraCfgMgr(20806): Loading module com.google.android.gms.maps from APK /data/data/com.google.android.gms/app_chimera/chimera-module-root/module-71c764a6f3cb92bdc5525a965b589e7c5ed304f3/MapsModule.apk
02-15 12:56:21.950: D/ChimeraModuleLdr(20806): Loading module APK /data/data/com.google.android.gms/app_chimera/chimera-module-root/module-71c764a6f3cb92bdc5525a965b589e7c5ed304f3/MapsModule.apk
02-15 12:56:21.950: D/ChimeraFileApk(20806): Primary ABI of requesting process is armeabi-v7a
02-15 12:56:21.950: I/art(20806): DexFile_isDexOptNeeded failed to open oat file '/data/dalvik-cache/arm/data@data@com.google.android.gms@app_chimera@chimera-module-root@module-71c764a6f3cb92bdc5525a965b589e7c5ed304f3@MapsModule.apk@classes.dex' for file location '/data/data/com.google.android.gms/app_chimera/chimera-module-root/module-71c764a6f3cb92bdc5525a965b589e7c5ed304f3/MapsModule.apk': Failed to open oat filename for reading: No such file or directory
02-15 12:56:21.960: D/ChimeraFileApk(20806): Classloading successful. Optimized code found.
02-15 12:56:22.040: I/Google Maps Android API(20806): Google Play services client version: 8487000
02-15 12:56:22.050: I/Google Maps Android API(20806): Google Play services package version: 8489238
02-15 12:56:22.220: I/e(20806): Token loaded from file. Expires in: 419712815 ms.
02-15 12:56:22.220: I/e(20806): Scheduling next attempt in 419412 seconds.
02-15 12:56:22.350: D/AbsListView(20806): Get MotionRecognitionManager
02-15 12:56:22.420: W/ContextImpl(20806): Failed to ensure directory: /storage/extSdCard/Android/data/com.example.birdview/cache
02-15 12:56:22.450: W/ActivityThread(20806): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
02-15 12:56:22.460: I/System.out(20806): (HTTPLog)-Static: isSBSettingEnabled false
02-15 12:56:22.460: I/System.out(20806): (HTTPLog)-Static: isShipBuild true
02-15 12:56:22.460: I/System.out(20806): (HTTPLog)-Thread-88543-829859576: SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
02-15 12:56:22.460: I/System.out(20806): (HTTPLog)-Static: isSBSettingEnabled false
02-15 12:56:22.540: I/System.out(20806): KnoxVpnUidStorageknoxVpnSupported API value returned is false
02-15 12:56:22.570: D/OpenGLRenderer(20806): Render dirty regions requested: true
02-15 12:56:22.610: I/Adreno-EGL(20806): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
02-15 12:56:22.610: I/Adreno-EGL(20806): OpenGL ES Shader Compiler Version: E031.25.03.06
02-15 12:56:22.610: I/Adreno-EGL(20806): Build Date: 01/24/15 Sat
02-15 12:56:22.610: I/Adreno-EGL(20806): Local Branch: AF11_RB1_AU15
02-15 12:56:22.610: I/Adreno-EGL(20806): Remote Branch: 
02-15 12:56:22.610: I/Adreno-EGL(20806): Local Patches: 
02-15 12:56:22.610: I/Adreno-EGL(20806): Reconstruct Branch: 
02-15 12:56:22.620: I/OpenGLRenderer(20806): Initialized EGL, version 1.4
02-15 12:56:22.660: D/OpenGLRenderer(20806): Enabling debug mode 0
02-15 12:56:22.780: I/Timeline(20806): Timeline: Activity_idle id: android.os.BinderProxy@2135111a time:286565164
02-15 12:56:23.061: I/art(20806): Background sticky concurrent mark sweep GC freed 33313(2MB) AllocSpace objects, 42(7MB) LOS objects, 12% free, 22MB/25MB, paused 14.770ms total 113.891ms
02-15 12:56:23.511: I/art(20806): Background partial concurrent mark sweep GC freed 1231(40KB) AllocSpace objects, 13(21MB) LOS objects, 37% free, 26MB/42MB, paused 7.263ms total 83.862ms
02-15 12:56:23.731: I/System.out(20806): (HTTPLog)-Static: isSBSettingEnabled false
02-15 12:56:23.852: I/System.out(20806): (HTTPLog)-Static: isSBSettingEnabled false
02-15 12:56:27.535: I/System.out(20806): KnoxVpnUidStorageknoxVpnSupported API value returned is false
02-15 12:56:32.510: D/Volley(20806): [88545] a.a: HTTP response for request=<[ ] https://csi.gstatic.com/csi?s=maps_android_api&v=3&action=map_start_up&it=map_load.1513,on_create.1,on_resume.1,init.323&irt=1513,324,327,323 0x4d844933 NORMAL 2> [lifetime=8773], [size=0], [rc=204], [retryCount=0]
02-15 12:56:32.540: D/Volley(20806): [1] p.b: 8812 ms: [ ] https://csi.gstatic.com/csi?s=maps_android_api&v=3&action=map_start_up&it=map_load.1513,on_create.1,on_resume.1,init.323&irt=1513,324,327,323 0x4d844933 NORMAL 2
Todai
  • 164
  • 9
  • Just in case, maybe this topic will help you : http://stackoverflow.com/questions/25653898/google-maps-fragment-returning-null-inside-a-fragment – Arnaud Feb 15 '16 at 13:04
  • your are not initialized GoogleMap object – Naveen Tamrakar Feb 15 '16 at 13:04
  • samle code for u https://github.com/ErNaveen/google_map_sample – Naveen Tamrakar Feb 15 '16 at 13:05
  • Why don't you use Android Studio and it's wizard to create sample google maps project? – Mussa Feb 15 '16 at 13:11
  • @Berger have looked at that previously - and attempted some fixes. Nothing helped though. NaveenTamrakar: Explain how that would be an issue since the map is actually showing. Your sample (mapdemo) didn't work when taking snippets from it and pasting in mine (nullpointer) – Todai Feb 15 '16 at 13:13
  • @Mussa Simply because I wish to use Eclipse. I know it would be easier using Android studio, but the jobs on the market where I live that use Eclipse vastly outnumber the ones using AS. :) – Todai Feb 15 '16 at 13:16

1 Answers1

3

Your code has two problems:

First, this line:

googleMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

will always return null, because in your layout activity you don't have a fragment with id map it is your PlaceholderFragment fragment has MapFragment with id map. You need to use getChildFragmentManager method.

Second problem, your method onMapReady is never called, because you didn't register on your MapFragment with getMapAsync(this) method.

My suggestion for your problem:

Change your activity_startup to this:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="kz.ibragimov.googlemaps.MapsActivity" />

and your activity Startup to this:

package com.example.birdview;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class Startup extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startup);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        double latitude = 13.094192;
        double longitude = 80.121689;
        mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps"));
    }
}
Mussa
  • 1,463
  • 21
  • 25