1

我正在尝试使用 MapsForge 在地图上显示多个图标。我只能在地图上正确的 GPS 位置放置 2 个图标,而不会出现错误。当我尝试添加第三个图标时,应用程序崩溃并且出现空指针异常。此外,该应用程序不会下载瓷砖并给我“MapnikTileDownload:主机未解析: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));
}
}

这是我尝试添加第三个后的logcat:

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
4

2 回答 2

0

你没有初始化 List mapOverlays

在 setOverlay1 和 setOverlay2

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

mapOverlays = new List<Overlay>();

于 2013-02-22T04:21:32.130 回答
0

第 105 行:itemizedOverlay3.removeItem(itemizedOverlay3.size()-1);

此处生成 NPE 的唯一方法是 itemizedOverlay3 为空(因为它是唯一的对象)。考虑一下您的 if-else 语句:您在“if”部分初始化 itemizedOverlay3,但如果改为调用“else”呢?您正在调用 itemizedOverlay3,但它还没有被初始化

所以这是你的问题。在 if-else 语句之外对其进行初始化,使其在任何一种情况下都是有效的对象。(并且也为您的其他方法执行此操作,以避免在其中一个方法中弹出此错误。)

于 2013-02-22T05:37:31.863 回答