Mehdi Nazari Mehdi Nazari - 2 months ago 10
Android Question

can't make google map v2 to work

I tried to use Map v2. i got the api key using sha1 code. but every time i go to my activity my app crashes.

this is my manifest :

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


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

<permission
android:name="com.example.mehdi.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />

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

<uses-permission android:name="com.example.mehdi.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />



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

<meta-data
android:name="com.google.android.gms.v2.API_KEY"
android:value="my api key is here"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />


<activity
android:name=".Splash"
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>


this is my Splash activity :

package com.example.mehdi;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class Splash extends Activity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));

// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}





}


this is my splash.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"
tools:context=".Splash" >

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

</RelativeLayout>


i did import google-play-services_lib to my workspace and set it to library and used it in my package.

this is my logCat after running the app in emulator :

09-11 03:38:11.501: I/ActivityManager(1256): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.mehdi/.Splash} from pid 1406
09-11 03:38:11.521: D/dalvikvm(1256): GC_FOR_ALLOC freed 1061K, 24% free 7962K/10396K, paused 1ms, total 12ms
09-11 03:38:11.521: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.531: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.531: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.531: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.541: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.541: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressStandard.ogg
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressSpacebar.ogg
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressDelete.ogg
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressReturn.ogg
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressInvalid.ogg
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
09-11 03:38:11.551: W/AudioService(1256): onLoadSoundEffects(), Error -1 while loading samples
09-11 03:38:11.861: D/dalvikvm(2586): Not late-enabling CheckJNI (already on)
09-11 03:38:11.861: I/ActivityManager(1256): Start proc com.example.mehdi for activity com.example.mehdi/.Splash: pid=2586 uid=10059 gids={50059, 3003, 1028, 1015}
09-11 03:38:11.981: D/AndroidRuntime(2586): Shutting down VM
09-11 03:38:11.981: W/dalvikvm(2586): threadid=1: thread exiting with uncaught exception (group=0xb2d21b20)
09-11 03:38:11.981: E/AndroidRuntime(2586): FATAL EXCEPTION: main
09-11 03:38:11.981: E/AndroidRuntime(2586): Process: com.example.mehdi, PID: 2586
09-11 03:38:11.981: E/AndroidRuntime(2586): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mehdi/com.example.mehdi.Splash}: java.lang.NullPointerException
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.os.Handler.dispatchMessage(Handler.java:102)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.os.Looper.loop(Looper.java:136)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-11 03:38:11.981: E/AndroidRuntime(2586): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 03:38:11.981: E/AndroidRuntime(2586): at java.lang.reflect.Method.invoke(Method.java:515)
09-11 03:38:11.981: E/AndroidRuntime(2586): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-11 03:38:11.981: E/AndroidRuntime(2586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-11 03:38:11.981: E/AndroidRuntime(2586): at dalvik.system.NativeStart.main(Native Method)
09-11 03:38:11.981: E/AndroidRuntime(2586): Caused by: java.lang.NullPointerException
09-11 03:38:11.981: E/AndroidRuntime(2586): at com.example.mehdi.Splash.onCreate(Splash.java:25)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.Activity.performCreate(Activity.java:5231)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-11 03:38:11.981: E/AndroidRuntime(2586): ... 11 more
09-11 03:38:12.091: E/BufferQueue(918): [com.android.launcher/com.android.launcher2.Launcher] cancelBuffer: slot 0 is not owned by the client (state=3)
09-11 03:38:12.511: W/ActivityManager(1256): Force finishing activity com.example.mehdi/.Splash
09-11 03:38:12.841: I/WindowManager(1256): Screenshot max retries 4 of Token{b3355e18 ActivityRecord{b34e9d78 u0 com.example.mehdi/.Splash t7 f}} appWin=Window{b3374de0 u0 Starting com.example.mehdi} drawState=4
09-11 03:38:12.841: W/WindowManager(1256): Screenshot failure taking screenshot for (800x1280) to layer 21010
09-11 03:38:13.021: D/dalvikvm(1375): GC_FOR_ALLOC freed 399K, 26% free 4631K/6176K, paused 46ms, total 46ms
09-11 03:38:13.081: D/DropBoxEntryAddedChimeraService(1508): User is not opted-in to Usage & Diagnostics or Lockbox.
09-11 03:38:13.361: W/ActivityManager(1256): Activity pause timeout for ActivityRecord{b34e9d78 u0 com.example.mehdi/.Splash t7 f}


i also tried starting app on multiple android physical devices but all of them crashed.

i solved the problem. thanks to @gaurav4sarma.

solution:

i used api > 20 for this matter. lower api was the problem.
i needed to add support v7 to extend AppCompatActivity.

Answer

The error you are getting is an NPE error for the map fragment. The main reason is you are not setting the correct content layout inside the setContentView method inside onCreate.

setContentView(R.layout.activity_main); //Your Layout is splash.xml

Change it to

setContentView(R.layout.splash);

Even though this will solve the NPE issue. I would still recommend using the support library version of map and do then do your map operations inside the async callback for the map like below

<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"
tools:context=".MainActivity" >

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

</RelativeLayout>

And inside your OnCreate instantiate the map as follows

public class Splash  extends AppCompatActivity implements OnMapReadyCallback {

    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    static final LatLng KIEL = new LatLng(53.551, 9.993);
    SupportMapFragment mapFragment;

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

        mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                        .title("Hamburg"));
        Marker kiel = map.addMarker(new MarkerOptions()
                        .position(KIEL)
                        .title("Kiel")
                        .snippet("Kiel is cool")
                        .icon(BitmapDescriptorFactory
                                        .fromResource(R.drawable.ic_launcher)));

        // Move the camera instantly to hamburg with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

        // Zoom in, animating the camera.
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    }
}
Comments