无法在android谷歌地图中启动活动componentinfo java.lang.nullpointerexception?

时间:2013-08-14 07:59:33

标签: android google-maps

我试图通过检测用户当前位置来获取Android应用中最近的地方列表,我在列表视图中根据谷歌类型成功获取用户最近的地方。如果我点击按钮它我在地图上有显示地点的按钮正在显示以下错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nws.parkd/com.nws.parkd.map.PlacesMapActivity}: java.lang.NullPointerException

这是我的代码

map_places.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />    

PlacesMapActivity.java

public class PlacesMapActivity extends FragmentActivity {
// Nearest places
PlacesList nearPlaces;

// Map view
// MapView mapView;
private GoogleMap mapView;

// Map overlay items
List<Overlay> mapOverlays;

AddItemizedOverlay itemizedOverlay;

GeoPoint geoPoint;
// Map controllers
MapController mc;

double latitude;
double longitude;
OverlayItem overlayitem;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_places);
    MapFragment map = ((MapFragment) getFragmentManager().findFragmentById(
            R.id.mapView));
    mapView = map.getMap();

    // Getting intent data
    Intent i = getIntent();

    // Users current geo location
    String user_latitude = i.getStringExtra("user_latitude");
    String user_longitude = i.getStringExtra("user_longitude");

    // Nearplaces list
    nearPlaces = (PlacesList) i.getSerializableExtra("near_places");

    // mapView = (MapView) findViewById(R.id.mapView);
    // mapView.setBuiltInZoomControls(true);

    // mapOverlays = mapView.getOverlays();

    // Geopoint to place on map
    geoPoint = new GeoPoint(
            (int) (Double.parseDouble(user_latitude) * 1E6),
            (int) (Double.parseDouble(user_longitude) * 1E6));

    // Drawable marker icon
    Drawable drawable_user = this.getResources().getDrawable(
            R.drawable.mark_red);

    itemizedOverlay = new AddItemizedOverlay(drawable_user, this);

    // Map overlay item
    overlayitem = new OverlayItem(geoPoint, "Your Location", "That is you!");

    itemizedOverlay.addOverlay(overlayitem);

    mapOverlays.add(itemizedOverlay);
    itemizedOverlay.populateNow();

    // Drawable marker icon
    Drawable drawable = this.getResources().getDrawable(
            R.drawable.mark_blue);

    itemizedOverlay = new AddItemizedOverlay(drawable, this);

    // mc = mapView.getController();

    // These values are used to get map boundary area
    // The area where you can see all the markers on screen
    int minLat = Integer.MAX_VALUE;
    int minLong = Integer.MAX_VALUE;
    int maxLat = Integer.MIN_VALUE;
    int maxLong = Integer.MIN_VALUE;

    // check for null in case it is null
    if (nearPlaces.results != null) {
        // loop through all the places
        for (Place place : nearPlaces.results) {
            latitude = place.geometry.location.lat; // latitude
            longitude = place.geometry.location.lng; // longitude

            // Geopoint to place on map
            geoPoint = new GeoPoint((int) (latitude * 1E6),
                    (int) (longitude * 1E6));

            // Map overlay item
            overlayitem = new OverlayItem(geoPoint, place.name,
                    place.vicinity);

            itemizedOverlay.addOverlay(overlayitem);

            // calculating map boundary area
            minLat = (int) Math.min(geoPoint.getLatitudeE6(), minLat);
            minLong = (int) Math.min(geoPoint.getLongitudeE6(), minLong);
            maxLat = (int) Math.max(geoPoint.getLatitudeE6(), maxLat);
            maxLong = (int) Math.max(geoPoint.getLongitudeE6(), maxLong);
        }
        mapOverlays.add(itemizedOverlay);

        // showing all overlay items
        itemizedOverlay.populateNow();
    }

    // Adjusting the zoom level so that you can see all the markers on map
    // mapView.getController().zoomToSpan(Math.abs(minLat - maxLat),
    // Math.abs(minLong - maxLong));

    // Showing the center of the map
    mc.animateTo(new GeoPoint((maxLat + minLat) / 2,
            (maxLong + minLong) / 2));
    // mapView.postInvalidate();

}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the
    // map.
    if (mapView == null) {
        mapView = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.mapView)).getMap();
        // Check if we were successful in obtaining the map.
        if (mapView != null) {
            // The Map is verified. It is now safe to manipulate the map.

        }
    }
}

}

和logcat显示导致的错误

08-14 07:44:14.054: W/ResourceType(2051): Skipping entry 0x7f020026 in package table 0 because it    is not complex!
08-14 07:44:14.065: W/ResourceType(2051): Skipping entry 0x7f020026 in package table 0 because it is not complex!
08-14 07:44:14.074: W/ResourceType(2051): Skipping entry 0x7f020026 in package table 0 because it is not complex!
08-14 07:44:14.074: W/ResourceType(2051): Skipping entry 0x7f020026 in package table 0 because it is not complex!
08-14 07:44:14.464: D/gralloc_goldfish(2051): Emulator without GPU emulation detected.
08-14 07:44:17.295: D/dalvikvm(2051): GC_FOR_ALLOC freed 66K, 6% free 2913K/3096K, paused 98ms, total 128ms
08-14 07:44:17.327: I/dalvikvm-heap(2051): Grow heap (frag case) to 3.563MB for 635812-byte allocation
08-14 07:44:17.525: D/dalvikvm(2051): GC_FOR_ALLOC freed 7K, 6% free 3526K/3720K, paused 203ms, total 203ms
08-14 07:44:17.815: D/Network(2051): Network Enabled
08-14 07:44:17.826: D/GPS(2051): GPS Enabled
08-14 07:44:17.865: D/Your Location(2051): latitude:16.495558333333335, longitude: 80.66023166666668
08-14 07:44:18.945: I/Choreographer(2051): Skipped 90 frames!  The application may be doing too much work on its main thread.
08-14 07:44:19.285: I/Choreographer(2051): Skipped 73 frames!  The application may be doing too much work on its main thread.
08-14 07:44:20.215: I/Choreographer(2051): Skipped 199 frames!  The application may be doing too much work on its main thread.
08-14 07:44:22.575: I/Choreographer(2051): Skipped 36 frames!  The application may be doing too much work on its main thread.
08-14 07:44:23.025: I/Choreographer(2051): Skipped 44 frames!  The application may be doing too much work on its main thread.
08-14 07:44:26.535: D/dalvikvm(2051): GC_FOR_ALLOC freed 227K, 9% free 3812K/4156K, paused 108ms, total 108ms
08-14 07:44:26.576: I/Choreographer(2051): Skipped 34 frames!  The application may be doing too much work on its main thread.
08-14 07:44:26.666: I/Choreographer(2051): Skipped 32 frames!  The application may be doing too much work on its main thread.
08-14 07:44:28.695: I/Choreographer(2051): Skipped 41 frames!  The application may be doing too much work on its main thread.
08-14 07:44:29.094: I/Choreographer(2051): Skipped 40 frames!  The application may be doing too much work on its main thread.
08-14 07:44:33.774: D/dalvikvm(2051): GC_FOR_ALLOC freed 433K, 13% free 3890K/4440K, paused 114ms, total 114ms
08-14 07:44:33.824: I/Choreographer(2051): Skipped 35 frames!  The application may be doing too much work on its main thread.
08-14 07:44:34.284: D/Places Status(2051): OK
08-14 07:44:34.854: I/Choreographer(2051): Skipped 44 frames!  The application may be doing too much work on its main thread.
08-14 07:44:37.124: W/GooglePlayServicesUtil(2051): Google Play services out of date.  Requires 3159100 but found 3158130
08-14 07:44:37.225: W/GooglePlayServicesUtil(2051): Google Play services out of date.  Requires 3159100 but found 3158130
08-14 07:44:37.287: W/GooglePlayServicesUtil(2051): Google Play services out of date.  Requires 3159100 but found 3158130
08-14 07:44:37.364: W/GooglePlayServicesUtil(2051): Google Play services out of date.  Requires 3159100 but found 3158130
08-14 07:44:37.474: W/GooglePlayServicesUtil(2051): Google Play services out of date.  Requires 3159100 but found 3158130
08-14 07:44:37.545: D/AndroidRuntime(2051): Shutting down VM
08-14 07:44:37.554: W/dalvikvm(2051): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-14 07:44:37.755: E/AndroidRuntime(2051): FATAL EXCEPTION: main
08-14 07:44:37.755: E/AndroidRuntime(2051): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nws.parkd/com.nws.parkd.map.PlacesMapActivity}: java.lang.NullPointerException
08-14 07:44:37.755: E/AndroidRuntime(2051):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-14 07:44:37.755: E/AndroidRuntime(2051):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-14 07:44:37.755: E/AndroidRuntime(2051):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-14 07:44:37.755: E/AndroidRuntime(2051):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-14 07:44:37.755: E/AndroidRuntime(2051):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 07:44:37.755: E/AndroidRuntime(2051):     at android.os.Looper.loop(Looper.java:137)
08-14 07:44:37.755: E/AndroidRuntime(2051):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-14 07:44:37.755: E/AndroidRuntime(2051):     at java.lang.reflect.Method.invokeNative(Native Method)
08-14 07:44:37.755: E/AndroidRuntime(2051):     at java.lang.reflect.Method.invoke(Method.java:525)
08-14 07:44:37.755: E/AndroidRuntime(2051):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-14 07:44:37.755: E/AndroidRuntime(2051):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 07:44:37.755: E/AndroidRuntime(2051):     at dalvik.system.NativeStart.main(Native Method)
08-14 07:44:37.755: E/AndroidRuntime(2051): Caused by: java.lang.NullPointerException
08-14 07:44:37.755: E/AndroidRuntime(2051):     at com.nws.parkd.map.PlacesMapActivity.onCreate(PlacesMapActivity.java:55)
08-14 07:44:37.755: E/AndroidRuntime(2051):     at android.app.Activity.performCreate(Activity.java:5133)
08-14 07:44:37.755: E/AndroidRuntime(2051):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-14 07:44:37.755: E/AndroidRuntime(2051):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-14 07:44:37.755: E/AndroidRuntime(2051):     ... 11 more
08-14 07:49:38.025: I/Process(2051): Sending signal. PID: 2051 SIG: 9
08-14 07:49:38.875: W/ResourceType(2095): Skipping entry 0x7f020026 in package table 0 because it is not complex!
08-14 07:49:38.875: W/ResourceType(2095): Skipping entry 0x7f020026 in package table 0 because it is not complex!
08-14 07:49:38.885: W/ResourceType(2095): Skipping entry 0x7f020026 in package table 0 because it is not complex!
08-14 07:49:38.885: W/ResourceType(2095): Skipping entry 0x7f020026 in package table 0 because it is not complex!
08-14 07:49:39.215: D/gralloc_goldfish(2095): Emulator without GPU emulation detected.

2 个答案:

答案 0 :(得分:1)

DO

mapOverlays = new List<Overlay>() ;

before mapOverlays.add()

您尚未创建mapOverlays对象,因此它为null。

您正在尝试执行mapOverlays.add()因此异常

答案 1 :(得分:0)

请检查您的清单文件。你有没有添加所有必要的活动?