1

I'm trying to add google maps to my app. The logcat window shows this error: Caused by: java.lang.NullPointerException at com.example.mymaps.MainActivity.onCreate(MainActivity.java:27) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080).

How to remove this error?

   Main 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=".MainActivity" >

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

 </RelativeLayout>

Main Java 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 MainActivity 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.activity_main);
     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);
   }

    @Override
   public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu.activity_main, menu);
   return true;
   }

  } 

LogCat window:

08-13 23:32:27.279: E/AndroidRuntime(1010): FATAL EXCEPTION: main 08-13 23:32:27.279: E/AndroidRuntime(1010): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mymaps/com.example.mymaps.MainActivity}: java.lang.NullPointerException 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.ActivityThread.access$600(ActivityThread.java:141) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.os.Handler.dispatchMessage(Handler.java:99) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.os.Looper.loop(Looper.java:137) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.ActivityThread.main(ActivityThread.java:5039) 08-13 23:32:27.279: E/AndroidRuntime(1010): at java.lang.reflect.Method.invokeNative(Native Method) 08-13 23:32:27.279: E/AndroidRuntime(1010): at java.lang.reflect.Method.invoke(Method.java:511) 08-13 23:32:27.279: E/AndroidRuntime(1010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 08-13 23:32:27.279: E/AndroidRuntime(1010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08-13 23:32:27.279: E/AndroidRuntime(1010): at dalvik.system.NativeStart.main(Native Method) 08-13 23:32:27.279: E/AndroidRuntime(1010): Caused by: java.lang.NullPointerException 08-13 23:32:27.279: E/AndroidRuntime(1010): at com.example.mymaps.MainActivity.onCreate(MainActivity.java:27) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.Activity.performCreate(Activity.java:5104) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 08-13 23:32:27.279: E/AndroidRuntime(1010): ... 11 more

Rupinder
  • 25
  • 5
  • What line is line 27 in your `onCreate` method? – Andrea Thacker Aug 13 '15 at 22:39
  • That issue is resolved. But I cannot see any map in my phone. Just showing "zoom in" and "zoom out" symbols and "Google" on the left bottom. But there is no map. Why is this happening? – Rupinder Aug 13 '15 at 22:47

2 Answers2

0

It may be due to play service issue. So null check map object before using map so it will automatically ask to download map in device. please try and reply

    @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();
if(map!=null){
         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);
}
       }
Nivedh
  • 971
  • 1
  • 8
  • 19
  • Thank you so much. It is no more showing that exception. But when I installed the apk on my phone I cannot see any map. There are only zoom in and zoom out signs and in the left "Google " is getting displayed. But there is no map. Why is it so? – Rupinder Aug 13 '15 at 22:43
  • your mobile dosent have platservice. please download google playservice for map – Nivedh Aug 13 '15 at 22:46
  • I have google play service in my mobile. Still it is not working? – Rupinder Aug 13 '15 at 22:57
  • for me it was the issue with some device due to play service. are you facing this in some device or in all device?. please paste the logcat while it get null pointer exception – Nivedh Aug 13 '15 at 23:09
  • It is not working on any device. Is there any other alternative>? – Rupinder Aug 13 '15 at 23:30
  • Please check Is your api key generated from the system you working itself? – Nivedh Aug 14 '15 at 04:44
  • and check whether playservice library is added – Nivedh Aug 14 '15 at 05:30
0
  1. First check google map is null or not null using this method :

    If(gMap!=null)
    
  2. Whether you have to download marker icon form URL (or) add marker image to the marker info window directly from URL. It's possible to get the network main thread exception.

  3. Network main thread exception leads to marker image null so that you may get null pointer exception in google map

Solution:

add the following lines in your onCreate method

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy);
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
Mahendran Candy
  • 1,114
  • 17
  • 17