当前位置: 动力学知识库 > 问答 > 编程问答 >

android - NullPointerException in getOverlays() method

问题描述:

I am seeing an issue in the code below where everytime I call the "getOverlays()" method of the MapView (appropriately called mapView) I receive a NullPointerException. This is a consistent event, even when I pass the result of the "getOverlays()" call into a variable then carry out the "add(itemisedOverlay) onto that. I have used the code of the Hello, MapView tutorial as a basis and reference for this code, with a little bit of modification to allow for the usage of the GPS location (gleaned from my experiences with the OSMdroid libraries used in an alternative prototype of this application). The objective here is to display the last known GPS location via the maps API with a small icon at that location.

In addition if I remove the try/catch declaration below this becomes a app breaking error.

Any help with eliminating the NullPointerException will be much appreciated!

TestMapDisplayGoogleActivity:

package com.test.google;

import com.google.android.maps.GeoPoint;

import com.google.android.maps.MapActivity;

import com.google.android.maps.MapView;

import com.google.android.maps.OverlayItem;

import android.graphics.drawable.Drawable;

import android.location.Criteria;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

public class TestMapDisplayGoogleActivity extends MapActivity {

Drawable drawable;

TestItemizedOverlay itemisedOverlay;

private GeoPoint center;

LocationListener TestListener = new LocationListener()

{

public void onLocationChanged(Location location)

{

center = new GeoPoint((int) location.getLatitude(), (int) location.getLongitude());;

}

public void onProviderDisabled(String provider)

{

// required for interface, not used

}

public void onProviderEnabled(String provider)

{

// required for interface, not used

}

public void onStatusChanged(String provider, int status, Bundle extras)

{

// required for interface, not used

}

};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

try

{

//Create the mapview & set basic settings

MapView mapView = (MapView) findViewById(R.id.mapview);

//Retrieve location manager and last know GPS location

LocationManager locMan = (LocationManager) this.getSystemService(LOCATION_SERVICE);

locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2500, 100, TestListener);

Criteria testCrit = new Criteria();

testCrit.setAccuracy(Criteria.ACCURACY_COARSE);

Location location = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);

center = new GeoPoint((int) location.getLatitude(), (int) location.getLongitude());

//Instantiate the overlays and create the item to be added to the overlay

drawable = this.getResources().getDrawable(R.drawable.ic_launcher);

itemisedOverlay = new TestItemizedOverlay(drawable);

OverlayItem testItem = new OverlayItem(center, "Test", "Hey there!");

//Add testItem to the overlay

itemisedOverlay.addOverlayItem(testItem);

mapView.getOverlays().add(itemisedOverlay);

mapView.getController().setCenter(center);

mapView.getController().setZoom(20);

mapView.setSatellite(true);

setContentView(R.layout.main);

}

catch (NullPointerException e)

{

setContentView(R.layout.error_display);

e.printStackTrace();

}

}

@Override

protected boolean isRouteDisplayed() {

// TODO Auto-generated method stub

return false;

}

}

Mapview in main.xml:

 <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/mainlayout"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<com.google.android.maps.MapView

android:id="@+id/mapview"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:clickable="true"

android:apiKey="[Location of API Key in real project]"

/>

</RelativeLayout>

Error message:

11-02 03:53:00.294: E/AndroidRuntime(2589): FATAL EXCEPTION: main

11-02 03:53:00.294: E/AndroidRuntime(2589): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.google/com.test.google.TestMapDisplayGoogleActivity}: java.lang.NullPointerException

11-02 03:53:00.294: E/AndroidRuntime(2589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

11-02 03:53:00.294: E/AndroidRuntime(2589): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

11-02 03:53:00.294: E/AndroidRuntime(2589): at android.app.ActivityThread.access$2300(ActivityThread.java:125)

11-02 03:53:00.294: E/AndroidRuntime(2589): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

11-02 03:53:00.294: E/AndroidRuntime(2589): at android.os.Handler.dispatchMessage(Handler.java:99)

11-02 03:53:00.294: E/AndroidRuntime(2589): at android.os.Looper.loop(Looper.java:123)

11-02 03:53:00.294: E/AndroidRuntime(2589): at android.app.ActivityThread.main(ActivityThread.java:4627)

11-02 03:53:00.294: E/AndroidRuntime(2589): at java.lang.reflect.Method.invokeNative(Native Method)

11-02 03:53:00.294: E/AndroidRuntime(2589): at java.lang.reflect.Method.invoke(Method.java:521)

11-02 03:53:00.294: E/AndroidRuntime(2589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)

11-02 03:53:00.294: E/AndroidRuntime(2589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

11-02 03:53:00.294: E/AndroidRuntime(2589): at dalvik.system.NativeStart.main(Native Method)

11-02 03:53:00.294: E/AndroidRuntime(2589): Caused by: java.lang.NullPointerException

11-02 03:53:00.294: E/AndroidRuntime(2589): at com.test.google.TestMapDisplayGoogleActivity.onCreate(TestMapDisplayGoogleActivity.java:74)

11-02 03:53:00.294: E/AndroidRuntime(2589): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

11-02 03:53:00.294: E/AndroidRuntime(2589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

11-02 03:53:00.294: E/AndroidRuntime(2589): ... 11 more

网友答案:
setContentView(R.layout.main);

This line should be the first one in the OnCreate() because it contains the mapview so untill and unless you set the view

MapView mapView = (MapView) findViewById(R.id.mapview); will return NULL

分享给朋友:
您可能感兴趣的文章:
随机阅读: