Vikram Mahal
Vikram Mahal

Reputation: 306

Google map in android code giving null pointer exception

I am working on my project in which i need google map.. and i am using same code which i used in my previous app. but this time i am getting null pointer exception when i open this activity.

this is my map.java file :-

  public class map extends  FragmentActivity{
 GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);


    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.map);

    Bundle b=getIntent().getExtras();
    String lat[]=b.getStringArray("lat");
    String log[]=b.getStringArray("log");
    String name[]=b.getStringArray("name");
    FragmentManager fmanager = getSupportFragmentManager();
    Fragment fragment = fmanager.findFragmentById(R.id.map);
    SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
    map = supportmapfragment.getMap();




    LatLng place[]=new  LatLng[lat.length];
    for(int i=0;i<place.length;i++)
    {
        if(lat[i].equals(""))
            lat[i]="0.0";
        if(log[i].equals(""))
            log[i]="0.0";
         place[i]=new  LatLng(Double.parseDouble(lat[i]),Double.parseDouble(log[i]));
    }
    Marker marker[]=new Marker[lat.length];
    for(int i=0;i<lat.length;i++)
    {
        marker[i] = map.addMarker(new MarkerOptions().position(place[i]).title(name[i]));
    }

        // Move the camera instantly to hamburg with a zoom of 15.
    for(int i=0;i<place.length;i++)
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(place[i], 15));
        // Zoom in, animating the camera.
      map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);


}



  }

My XML FILE:-

     <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=".map" >

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

 </RelativeLayout>

MY MANIFEST FILE :-

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

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

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

       <uses-permission     android:name="com.pumaguard.pumaguard.locationapi.maps.permission.MAPS_RECEIVE" />
       <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" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.pumaguard.pumaguard.splash"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar" >
        </activity>
        <activity
            android:name=".newactivity"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar" >
        </activity>

          <activity
            android:name=".map"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar" >
        </activity>

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

      </manifest>

lOGCAT ERROR:-

01-24 22:56:01.636: E/AndroidRuntime(835): FATAL EXCEPTION: main
01-24 22:56:01.636: E/AndroidRuntime(835): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pumaguard.pumaguard/com.pumaguard.pumaguard.map}: java.lang.NullPointerException
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.os.Looper.loop(Looper.java:137)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.ActivityThread.main(ActivityThread.java:5103)
01-24 22:56:01.636: E/AndroidRuntime(835):  at java.lang.reflect.Method.invokeNative(Native Method)
01-24 22:56:01.636: E/AndroidRuntime(835):  at java.lang.reflect.Method.invoke(Method.java:525)
01-24 22:56:01.636: E/AndroidRuntime(835):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-24 22:56:01.636: E/AndroidRuntime(835):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-24 22:56:01.636: E/AndroidRuntime(835):  at dalvik.system.NativeStart.main(Native Method)
01-24 22:56:01.636: E/AndroidRuntime(835): Caused by: java.lang.NullPointerException
01-24 22:56:01.636: E/AndroidRuntime(835):  at com.pumaguard.pumaguard.map.onCreate(map.java:62)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.Activity.performCreate(Activity.java:5133)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-24 22:56:01.636: E/AndroidRuntime(835):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-24 22:56:01.636: E/AndroidRuntime(835):  ... 11 more

SO, THIS IS MY CODE PLEASE HELP ME TO SORT OUT MY PROBLEM. THANKS.

Upvotes: 0

Views: 1069

Answers (1)

Ari
Ari

Reputation: 1316

First of all don't show your API key to everyone

Secondly you should generate API Key new for each and every app

and thirdly you can use

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = fm.getMap();

instead of

FragmentManager fmanager = getSupportFragmentManager();
Fragment fragment = fmanager.findFragmentById(R.id.map);
SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
map = supportmapfragment.getMap();

and tell us at which line you are getting the error, show us the line.

Upvotes: 1

Related Questions