添加标记时Android谷歌地图空指针异常

时间:2013-06-27 12:37:45

标签: java android google-maps google-maps-markers

我制作了一个简单的FragmentActivity,显示谷歌地图,并使用标记显示当前位置和附近的地方。但现在我需要在另一个应用程序中使用该代码,我遇到了这个问题。

这是活动

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);
}

当我打开地图时的日志:

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

活动布局:

<?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 个答案:

答案 0 :(得分:5)

我看不到您为userIcon分配任何值的位置,因此BitmapDescriptorFactory.fromResource(userIcon)可能会返回null

答案 1 :(得分:1)

  1. 首先使用此方法查看Google地图为空或无空 - &gt;如果(GMAP!= NULL)
  2. 是否必须直接从url.its下载标记图标表单网址(或)将标记图像添加到标记信息窗口,以获取网络主线程异常。
  3. 网络主线程异常会导致标记图像为null,因此您可能会在Google地图
  4. 中获得空指针异常

    解决方案:

    在onCreate方法中添加以下行

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

    StrictMode.setThreadPolicy(策略);