LeanDev
LeanDev

Reputation: 251

java.lang.NullPointerException GoogleMaps V2

i just learning about android programming to show map. but when i run this code ,there's some error. Can anybody explain why this error occurs? And what I can do to fix this problem? thank you very much.

Stacktrace:

12-01 02:58:27.324: E/AndroidRuntime(1360): FATAL EXCEPTION: main
12-01 02:58:27.324: E/AndroidRuntime(1360): java.lang.NullPointerException
12-01 02:58:27.324: E/AndroidRuntime(1360):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at android.app.Activity.startActivityForResult(Activity.java:3390)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at android.app.Activity.startActivityForResult(Activity.java:3351)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at android.app.Activity.startActivity(Activity.java:3587)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at android.app.Activity.startActivity(Activity.java:3555)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at com.google.android.gms.dynamic.a$5.onClick(Unknown Source)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at android.view.View.performClick(View.java:4240)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at android.view.View$PerformClick.run(View.java:17721)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at android.os.Handler.handleCallback(Handler.java:730)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at android.os.Looper.loop(Looper.java:137)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at java.lang.reflect.Method.invokeNative(Native Method)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at java.lang.reflect.Method.invoke(Method.java:525)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-01 02:58:27.324: E/AndroidRuntime(1360):     at dalvik.system.NativeStart.main(Native Method)
12-01 02:58:29.088: I/Process(1360): Sending signal. PID: 1360 SIG: 9

MainActivity.java

package unai.skripsi.mymaps;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
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;

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

public class MainActivity extends FragmentActivity {

    static final LatLng RAGUNAN = new LatLng(-6.3039, 106.8267);
    static final LatLng TAMANMINI = new LatLng(-6.29436, 106.8859);
    private GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map= ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    if (map!=null){
    Marker ragunan = map.addMarker(new MarkerOptions().position(RAGUNAN)
        .title("Ragunan"));
    Marker tamanmini = map.addMarker(new MarkerOptions().position(TAMANMINI)
        .title("Taman mini")
        .snippet("Taman mini itu indah")
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(RAGUNAN, 15));
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

    }
}


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

 }

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

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

</RelativeLayout>

Manifest

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

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

<permission
    android:name="unai.skripsi.mymaps.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

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

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="unai.skripsi.mymaps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<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" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="unai.skripsi.mymaps.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>
    <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="My API KEY" />
</application>

</manifest>

Upvotes: 3

Views: 5055

Answers (4)

Siddharth_Vyas
Siddharth_Vyas

Reputation: 10100

In your xml file change this class="com.google.android.gms.maps.MapFragment" to android:name="com.google.android.gms.maps.SupportMapFragment"

Extend your activity to FragmentActivty and change

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap(); to   
map= ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();

Also check

if (map== null) 
            {
          map= ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();}

Hope this helps.

Upvotes: 4

Vishal Pawar
Vishal Pawar

Reputation: 4340

Above mentioned problem can occur if you have emulator or device in which google play services are not installed. I don't have perfect solution but I figured out some work around to save your app from crashing. In this case you have to follow simple steps 1. Override startActivityForResult(intent, requestcode) 2. In startActivityForResult add super call super.startActivityForResult in try catch and catch the NullPointerException

Easy its done Now you can trap the nullpointerexception here and add your error handling in catch

Upvotes: 1

Shailendra Madda
Shailendra Madda

Reputation: 21561

And make sure following steps done correct or not:

Steps:

  • to ensure that device has Google Play services APK
  • to install Google Play Service rev. more than 2

enter image description here

  • to create project at https://code.google.com/apis/console/
  • to enable "Google Maps Android API v2" enter image description here
  • to register of SHA1 in project (NOW, YOU NEED WRITE SHA1;your.app.package.name) at APIs console and get API KEY
  • to copy directory ANDROID_SDK_DIR/extras/google/google_play_services/libproject/google-play-services_lib to root of your project
  • to add next line to the YOUR_PROJECT/project.properties

android.library.reference.1=google-play-services_lib

  • to add next lines to the YOUR_PROJECT/proguard-project.txt

-keep class * extends java.util.ListResourceBundle {

protected Object[][] getContents();

}

Okay, now you ready to create your own Google Map app with using Google Map APIs V2 for Android.

If you create application with min SDK = 8, please use android support library v4 + SupportMapFragment instead of MapFragment.

See here,just change the api key with your key in manifest file and follow these steps: and make sure that generate api key with package name which is mentioned in android manifest file and your google_play_services_lib project should be present in your project's work space only.

Manifest file:

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

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

    <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" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.geeklabs.map.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>

        <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="replace with your API key"/>

    </application>

</manifest>

MainActivity.java:

    package com.geeklabs.map;

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

public class MainActivity extends Activity {

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

}

activity_main.xml:

    <?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"/>

After got this let me know.

Upvotes: 1

sulfurid
sulfurid

Reputation: 1

If you are testing this programm in device, your programm will works with Level 17 or higher, to use from 2.3.3 try to set your code as Siddharth Vyas Advice : Never show your map key to every body.

Upvotes: 0

Related Questions