Android 安卓-谷歌地图获得;空指针异常";

Android 安卓-谷歌地图获得;空指针异常";,android,google-maps,maps,Android,Google Maps,Maps,我正在尝试使用MapsForge在地图上显示多个图标。我只能将2个图标放在地图上正确的GPS位置,而没有出现错误。当我尝试添加第三个图标时,应用程序崩溃,我得到一个空指针异常。此外,该应用程序没有下载互动程序,并给我“MapnikTileDownload:Host is unresolved:tile.openstreetmap.org:80” MapsForgeViewer: public class MapsForgeViewer extends MapActivity implements

我正在尝试使用MapsForge在地图上显示多个图标。我只能将2个图标放在地图上正确的GPS位置,而没有出现错误。当我尝试添加第三个图标时,应用程序崩溃,我得到一个空指针异常。此外,该应用程序没有下载互动程序,并给我“MapnikTileDownload:Host is unresolved:tile.openstreetmap.org:80”

MapsForgeViewer:

public class MapsForgeViewer extends MapActivity implements OnClickListener {

private List<Overlay> mapOverlays;
private Drawable drawable1, drawable2;
private CustomItemizedOverlay itemizedOverlay1, itemizedOverlay2, itemizedOverlay3;
private boolean isDisplayed = false;

private MapView mapView;
    private Button info;
    private Button roster;
    private Button schedule;
    private Button stats;
    private Button exit;

private OverlayItem [] pittIcon = {
        new OverlayItem( new GeoPoint(40.443061,-79.962273), "Pitt", "Panthers")       
    };
private OverlayItem [] cuseIcon = {
        new OverlayItem( new GeoPoint(43.037628,-76.137654), "Cuse", "Orangemen")
};
private OverlayItem [] lvilleIcon = {
        new OverlayItem( new GeoPoint(38.253153,-85.753847), "Louisville", "Cards")
};

    public void setOverlay1(){  
        int pittLength = pittIcon.length;
        // Create if it doesn't exist and display
        mapOverlays = mapView.getOverlays();    
        drawable1 = this.getResources().getDrawable(R.drawable.pittspin); 
        itemizedOverlay1 = new CustomItemizedOverlay(drawable1); 

        for(int i=0; i<pittLength; i++){
            itemizedOverlay1.addOverlay(pittIcon[i]);
        }
        mapOverlays.add(itemizedOverlay1); 
        // Added symbols will be displayed when map is redrawn so force redraw now
        mapView.postInvalidate(); 
    }

    public void setOverlay2(){  
        // Create itemizedOverlay2 if it doesn't exist
        mapOverlays = mapView.getOverlays();    
        drawable2 = this.getResources().getDrawable(R.drawable.louispin);
        itemizedOverlay2 = new CustomItemizedOverlay(drawable2);     
        // Add items
        itemizedOverlay2.addOverlay(lvilleIcon[itemizedOverlay2.size()]);   
        mapOverlays.add(itemizedOverlay2);      
        mapView.postInvalidate();
    }

    public void setOverlay3(){  
        int cuseLength = cuseIcon.length;
        // Create itemizedOverlay2 if it doesn't exist and display all three items
        mapOverlays = mapView.getOverlays();    
        Drawable drawable3 = this.getResources().getDrawable(R.drawable.cusepin); 
        itemizedOverlay3 = new CustomItemizedOverlay(drawable3); 
        // Display all three items at once
        for(int i=0; i<cuseLength; i++){
            itemizedOverlay3.addOverlay(cuseIcon[i]);
        }
        mapOverlays.add(itemizedOverlay3);
        isDisplayed = !isDisplayed;    
        // Added symbols will be displayed when map is redrawn so force redraw now
        mapView.postInvalidate(); 
    }

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.map_view);

        info = (Button) findViewById(R.id.information);
        roster = (Button) findViewById(R.id.roster);
        schedule = (Button) findViewById(R.id.schedule);
        stats = (Button) findViewById(R.id.stats);
        exit = (Button) findViewById(R.id.exit);
        info.setOnClickListener(this);
        roster.setOnClickListener(this);
        schedule.setOnClickListener(this);
        stats.setOnClickListener(this);
        exit.setOnClickListener(this);
        // end of mapView layout setup

        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setMapViewMode(MapViewMode.MAPNIK_TILE_DOWNLOAD);
        mapView.setBuiltInZoomControls(true);
        mapView.setScaleBar(true);
        mapView.setClickable(true);
        mapView.getController().setZoom(5);
        setCenterlocation();
        // end of mapView setup

        setOverlay1(); 
        setOverlay2();
        //setOverlay3();
    }

public void onClick(View v)
{
    switch (v.getId())
    {
        case (R.id.information):
            Intent intent = new Intent(getApplicationContext(), Information.class);
            startActivity(intent);
            break;

        case (R.id.roster):
            showRoster(v);
            break;

        case (R.id.schedule):
            break;

        case (R.id.stats):
            break;

        // close the app
        case (R.id.exit):
            finish();
            break;
    }
}

public void showRoster(View v) {
    String url = "www.espn.com/roster";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
} 

@Override
// resumes the actions of the application on a pause
protected void onResume()
{
    super.onResume();
}

// perform any cleanup before the activity is destroyed 
@Override
protected void onDestroy() {
    Cleanup();
    super.onDestroy();
}

private void Cleanup()
{    
    System.gc();
    Runtime.getRuntime().gc();  
}

// sets the center of the screen on the map
protected void setCenterlocation()
{
    mapView.getController().setCenter(new GeoPoint(38.00, -100.00));
}
}

您没有初始化列表映射覆盖

在setOverlay1和setOverlay2上

您应该在mapOverlays=mapView.getOverlays()之前初始化列表

mapOverlays=新列表()


第105行:itemizedOverlay3.removietem(itemizedOverlay3.size()-1)

这里生成NPE的唯一方法是itemizedOverlay3为null(因为它是唯一的对象)。考虑一下if if语句:在IF部分中初始化ItMeIZEdOffLay3,但如果调用“EFER”又如何呢?您正在调用itemizedOverlay3,但它尚未初始化


这就是你的问题。在if-else语句之外初始化它,使其在任何情况下都是有效的对象。(对其他方法也要这样做,以避免在其中一个方法中出现此错误。)

mapOverlays不需要初始化就可以设置为mapView对象。没有必要。是什么使setOverlay3与数字1和2不同?此外,它还精确地指出了第105行的空指针-这是哪一行?第105行:itemizedOverlay3.removietem(itemizedOverlay3.size()-1);啊哈,那很有帮助!请参阅下面的答案。这修复了我的空指针异常。我更改了代码以在不使用if-else语句的情况下初始化它们。现在我遇到错误,我的虚拟机无法分配足够的字节来执行此操作。当它是3个中等大小的图像时,这怎么可能呢?我已尝试增加.ini文件中的堆,但没有任何帮助。在不知道这些“中等”大小的图像有多大的情况下,我会说您需要使用“小”图像,因为mapView将多次绘制它们,这会增加所需的内存。尝试使用较小的测试图像,看看这是否解决了问题。
02-21 22:31:17.031: I/Process(597): Sending signal. PID: 597 SIG: 9
02-21 22:32:47.502: D/dalvikvm(633): GC_EXTERNAL_ALLOC freed 865 objects / 62432 bytes in 155ms
02-21 22:33:00.911: D/dalvikvm(633): GC_EXTERNAL_ALLOC freed 296 objects / 15424 bytes in 199ms
02-21 22:33:02.051: D/dalvikvm(633): GC_EXTERNAL_ALLOC freed 411 objects / 23800 bytes in 95ms
02-21 22:33:02.201: D/dalvikvm(633): GC_EXTERNAL_ALLOC freed 102 objects / 4240 bytes in 104ms
02-21 22:33:03.641: D/dalvikvm(633): GC_FOR_MALLOC freed 5686 objects / 1276552 bytes in 142ms
02-21 22:33:04.351: D/dalvikvm(633): GC_EXTERNAL_ALLOC freed 4507 objects / 992040 bytes in 126ms
02-21 22:33:05.092: D/dalvikvm(633): GC_EXTERNAL_ALLOC freed 510 objects / 25744 bytes in 128ms
02-21 22:33:05.532: D/dalvikvm(633): GC_EXTERNAL_ALLOC freed 83 objects / 3840 bytes in 243ms
02-21 22:33:05.732: D/dalvikvm(633): GC_EXTERNAL_ALLOC freed 3 objects / 80 bytes in 181ms
02-21 22:33:06.262: D/dalvikvm(633): GC_EXTERNAL_ALLOC freed 65 objects / 2504 bytes in 183ms
02-21 22:33:06.554: D/dalvikvm(633): GC_EXTERNAL_ALLOC freed 8 objects / 432 bytes in 289ms
02-21 22:33:26.285: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.291: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.341: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.374: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.393: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.421: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.443: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.451: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.471: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.471: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.491: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.511: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.531: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.541: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.552: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.561: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.572: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.592: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.592: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.611: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.622: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.633: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.641: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.661: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.661: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:33:26.681: D/osm(633): MapnikTileDownload: Host is unresolved: tile.openstreetmap.org:80
02-21 22:48:07.212: D/dalvikvm(667): GC_EXTERNAL_ALLOC freed 892 objects / 63472 bytes in 326ms
02-21 22:48:15.462: D/dalvikvm(667): GC_EXTERNAL_ALLOC freed 297 objects / 15424 bytes in 214ms
02-21 22:48:16.386: D/dalvikvm(667): GC_EXTERNAL_ALLOC freed 412 objects / 23824 bytes in 79ms
02-21 22:48:16.511: D/dalvikvm(667): GC_EXTERNAL_ALLOC freed 102 objects / 4240 bytes in 79ms
02-21 22:48:17.574: D/dalvikvm(667): GC_FOR_MALLOC freed 5685 objects / 1276512 bytes in 104ms
02-21 22:48:18.061: D/dalvikvm(667): GC_EXTERNAL_ALLOC freed 4507 objects / 992144 bytes in 89ms
02-21 22:48:18.441: D/AndroidRuntime(667): Shutting down VM
02-21 22:48:18.441: W/dalvikvm(667): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-21 22:48:18.473: E/AndroidRuntime(667): FATAL EXCEPTION: main
02-21 22:48:18.473: E/AndroidRuntime(667): java.lang.RuntimeException: Unable to start activity ComponentInfo{ashworth.craig/uc.roadmap.MapsForgeViewer}: java.lang.NullPointerException
02-21 22:48:18.473: E/AndroidRuntime(667):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-21 22:48:18.473: E/AndroidRuntime(667):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-21 22:48:18.473: E/AndroidRuntime(667):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-21 22:48:18.473: E/AndroidRuntime(667):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-21 22:48:18.473: E/AndroidRuntime(667):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 22:48:18.473: E/AndroidRuntime(667):  at android.os.Looper.loop(Looper.java:123)
02-21 22:48:18.473: E/AndroidRuntime(667):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-21 22:48:18.473: E/AndroidRuntime(667):  at java.lang.reflect.Method.invokeNative(Native Method)
02-21 22:48:18.473: E/AndroidRuntime(667):  at java.lang.reflect.Method.invoke(Method.java:521)
02-21 22:48:18.473: E/AndroidRuntime(667):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-21 22:48:18.473: E/AndroidRuntime(667):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-21 22:48:18.473: E/AndroidRuntime(667):  at dalvik.system.NativeStart.main(Native Method)
02-21 22:48:18.473: E/AndroidRuntime(667): Caused by: java.lang.NullPointerException
02-21 22:48:18.473: E/AndroidRuntime(667):  at uc.roadmap.MapsForgeViewer.setOverlay3(MapsForgeViewer.java:105)
02-21 22:48:18.473: E/AndroidRuntime(667):  at uc.roadmap.MapsForgeViewer.onCreate(MapsForgeViewer.java:185)
02-21 22:48:18.473: E/AndroidRuntime(667):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-21 22:48:18.473: E/AndroidRuntime(667):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-21 22:48:18.473: E/AndroidRuntime(667):  ... 11 more