4

I've made a simple FragmentActivity displaying google map and using markers to show current position and nearby places. But now I need to use that code inside another app, and I'm getting this problem.

This is the Activity

private void updatePlaces(JSONArray jDestinationArray) throws JSONException{

    locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Location lastLoc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    double lat = lastLoc.getLatitude();
    double lng = lastLoc.getLongitude();
    lastLatLng = new LatLng(lat, lng);

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    Builder boundsBuilder = new LatLngBounds.Builder();
    boundsBuilder.include(lastLatLng);

    markers = new HashMap<Marker,Object>();
    userMarker = map.addMarker(new MarkerOptions()
        .position(lastLatLng)
        .title("You are here")
        .icon(BitmapDescriptorFactory.fromResource(userIcon))
        .snippet("This is your last recorded location"));


    markers.put(userMarker, lastLatLng);
    for(int i=0; i<jDestinationArray.length(); i++){
        JSONObject destinationObject = jDestinationArray.getJSONObject(i);
        LatLng latlng = new LatLng(destinationObject.getDouble("latitude"), destinationObject.getDouble("longitude"));
        Marker destinationMarker=
                 map.addMarker(new MarkerOptions()
                .position(latlng)
                .title(destinationObject.getString("name"))
                .icon(BitmapDescriptorFactory.fromResource(destinationIcon))
                .snippet(destinationObject.getString("street"))
                );
        markers.put(destinationMarker, latlng);
        boundsBuilder.include(latlng);
    }

    // pan to see all markers on map:
    LatLngBounds bounds = boundsBuilder.build();
    map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, width, height, 30));
    //map.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng), 3000, null);
}

And the log, when I open the map:

06-27 14:18:28.530: D/dalvikvm(3545): GC_CONCURRENT freed 755K, 10% free 10520K/11655K, paused 13ms+4ms, total 50ms
06-27 14:18:28.530: D/dalvikvm(3545): WAIT_FOR_CONCURRENT_GC blocked 33ms
06-27 14:18:28.580: D/dalvikvm(3545): GC_FOR_ALLOC freed 756K, 11% free 10396K/11655K, paused 26ms, total 26ms
06-27 14:18:28.680: D/dalvikvm(3545): GC_CONCURRENT freed 690K, 11% free 10437K/11655K, paused 2ms+14ms, total 56ms
06-27 14:18:28.680: D/dalvikvm(3545): WAIT_FOR_CONCURRENT_GC blocked 21ms
06-27 14:18:28.710: W/ResourceType(3545): No package identifier when getting value for resource number 0x00000000
06-27 14:18:28.710: D/AndroidRuntime(3545): Shutting down VM
06-27 14:18:28.710: W/dalvikvm(3545): threadid=1: thread exiting with uncaught exception (group=0x40c42318)
06-27 14:18:28.730: E/AndroidRuntime(3545): FATAL EXCEPTION: main
06-27 14:18:28.730: E/AndroidRuntime(3545): java.lang.NullPointerException
06-27 14:18:28.730: E/AndroidRuntime(3545):     at maps.aj.bk.a(Unknown Source)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at maps.aj.bk.a(Unknown Source)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at maps.aj.al.a(Unknown Source)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at bcn.onTransact(SourceFile:167)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at android.os.Binder.transact(Binder.java:326)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addMarker(Unknown Source)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at com.example.foodexp03a.MainActivity.updatePlaces(MainActivity.java:330)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at com.example.foodexp03a.MainActivity.access$1(MainActivity.java:312)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at com.example.foodexp03a.MainActivity$LongRunningGetIO.onPostExecute(MainActivity.java:394)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at com.example.foodexp03a.MainActivity$LongRunningGetIO.onPostExecute(MainActivity.java:1)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at android.os.AsyncTask.finish(AsyncTask.java:631)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at android.os.Looper.loop(Looper.java:137)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at android.app.ActivityThread.main(ActivityThread.java:4744)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at java.lang.reflect.Method.invokeNative(Native Method)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at java.lang.reflect.Method.invoke(Method.java:511)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-27 14:18:28.730: E/AndroidRuntime(3545):     at dalvik.system.NativeStart.main(Native Method)
06-27 14:18:28.900: D/dalvikvm(3545): GC_FOR_ALLOC freed 274K, 7% free 10937K/11655K, paused 62ms, total 62ms
06-27 14:18:28.980: D/dalvikvm(3545): GC_CONCURRENT freed 65K, 7% free 10945K/11655K, paused 12ms+14ms, total 82ms
06-27 14:18:28.980: D/dalvikvm(3545): WAIT_FOR_CONCURRENT_GC blocked 1ms
06-27 14:18:29.010: D/dalvikvm(3545): GC_FOR_ALLOC freed 26K, 7% free 10918K/11655K, paused 31ms, total 31ms
06-27 14:18:29.020: I/dalvikvm-heap(3545): Grow heap (frag case) to 11.700MB for 1048592-byte allocation
06-27 14:18:29.050: D/dalvikvm(3545): GC_FOR_ALLOC freed <1K, 7% free 11942K/12743K, paused 30ms, total 30ms
06-27 14:18:29.110: D/dalvikvm(3545): GC_CONCURRENT freed <1K, 6% free 11981K/12743K, paused 2ms+23ms, total 57ms
06-27 14:18:29.201: D/dalvikvm(3545): GC_FOR_ALLOC freed 1219K, 15% free 10945K/12743K, paused 38ms, total 38ms
06-27 14:18:29.211: I/dalvikvm-heap(3545): Grow heap (frag case) to 11.725MB for 1048592-byte allocation
06-27 14:18:29.231: D/dalvikvm(3545): GC_FOR_ALLOC freed 1K, 7% free 11967K/12743K, paused 28ms, total 28ms
06-27 14:18:29.321: D/dalvikvm(3545): GC_CONCURRENT freed 1136K, 12% free 11290K/12743K, paused 13ms+20ms, total 82ms
06-27 14:18:29.461: D/dalvikvm(3545): GC_CONCURRENT freed 48K, 4% free 12305K/12743K, paused 27ms+14ms, total 93ms
06-27 14:18:29.461: D/dalvikvm(3545): WAIT_FOR_CONCURRENT_GC blocked 33ms
06-27 14:18:29.641: D/dalvikvm(3545): GC_FOR_ALLOC freed 2623K, 22% free 10873K/13831K, paused 31ms, total 32ms
06-27 14:18:29.641: I/dalvikvm-heap(3545): Grow heap (frag case) to 11.656MB for 1048592-byte allocation
06-27 14:18:29.671: D/dalvikvm(3545): GC_FOR_ALLOC freed 1K, 15% free 11895K/13831K, paused 37ms, total 37ms
06-27 14:18:29.721: D/dalvikvm(3545): GC_CONCURRENT freed 75K, 14% free 11898K/13831K, paused 2ms+4ms, total 45ms
06-27 14:18:29.801: D/dalvikvm(3545): GC_FOR_ALLOC freed 2113K, 22% free 10892K/13831K, paused 26ms, total 26ms
06-27 14:18:29.801: I/dalvikvm-heap(3545): Grow heap (frag case) to 11.674MB for 1048592-byte allocation
06-27 14:18:29.831: D/dalvikvm(3545): GC_FOR_ALLOC freed 4K, 14% free 11912K/13831K, paused 26ms, total 26ms
06-27 14:18:29.891: D/dalvikvm(3545): GC_CONCURRENT freed 21K, 14% free 11917K/13831K, paused 13ms+7ms, total 63ms
06-27 14:18:30.021: D/dalvikvm(3545): GC_CONCURRENT freed 2261K, 21% free 10952K/13831K, paused 12ms+8ms, total 66ms
06-27 14:18:30.021: D/dalvikvm(3545): WAIT_FOR_CONCURRENT_GC blocked 1ms
06-27 14:18:31.443: I/Process(3545): Sending signal. PID: 3545 SIG: 9

Layout of the activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <fragment android:name="com.example.foodexp03a.HomeFragment"
          android:id="@+id/homeFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
    <fragment android:name="com.example.foodexp03a.LoginFragment"
          android:id="@+id/loginFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
    <fragment android:name="com.example.foodexp03a.SettingsFragment"
          android:id="@+id/settingsFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

    <fragment android:name="com.example.foodexp03a.DestinationFragment"
          android:id="@+id/destinationFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
    <fragment
          android:id="@+id/mapFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
2
  • post your stack trace and narrow down your code to the relevant part. Commented Jun 27, 2013 at 12:39
  • Which line is "MainActivity.java:330" ? Commented Jun 27, 2013 at 12:45

2 Answers 2

5

I do not see where you assign userIcon any value, and therefore BitmapDescriptorFactory.fromResource(userIcon) might be returning null as a result.

Sign up to request clarification or add additional context in comments.

1 Comment

You are absolutely right! Next time I'll import code with more attention. Thanks a lot!
1
  1. First check google map is null or notnull 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 the url.its 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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.