0

我在我的应用程序中找到了地点列表,并且我已将纬度和经度传递给另一个类以在地图上显示该位置,但在单击列出的地点后显示“强制关闭应用程序”。

代码是:
使用 onClick 列出地点

 ListView lv = getListView();
            // listening to single list item click
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem

            String lat = ((TextView) view.findViewById(R.id.lat)).getText().toString();
            String lng = ((TextView) view.findViewById(R.id.lng)).getText().toString();
            // Starting new intent
            Intent in = new Intent(getApplicationContext(), ViewOnMap.class);

            in.putExtra(KEY_LAT, lat);
            in.putExtra(KEY_LONG, lng);
            startActivity(in);

        }
    });

然后我创建了另一个类来显示地图上的位置:

public class ViewOnMap extends MapActivity {

static final String KEY_LAT = "lat";
static final String KEY_LONG = "lng";

MapView mview;
MapController mcontrol;
LocationManager locmanager;
Location location;
GeoPoint geopoint;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapview);

     Intent in = getIntent();

    List<Overlay> mapOverlays = mview.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
    MapOverlay itemizedoverlay = new MapOverlay(drawable, this);


      String lat = in.getStringExtra(KEY_LAT);
      String lng = in.getStringExtra(KEY_LONG);

      int latitude = Integer.parseInt(lat);
      int longitude = Integer.parseInt(lng);

      mview =(MapView)findViewById(R.id.mapv);
      mview.displayZoomControls(true);
      mview.setBuiltInZoomControls(true);
      mcontrol =mview.getController();
      mcontrol.setZoom(9);
      GeoPoint point = new GeoPoint(latitude,longitude);
      OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");

      itemizedoverlay.addOverlay(overlayitem);
      mapOverlays.add(itemizedoverlay);

}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}

请告诉我是什么错误。这些是 LogCat 中的错误:

04-17 18:07:54.509: E/AndroidRuntime(13783): 致命异常: main 04-17 18:07:54.509: E/AndroidRuntime(13783): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. run.trial/com.run.trial.ViewOnMap}:java.lang.NullPointerException 04-17 18:07:54.509:E/AndroidRuntime(13783):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768) 04 -17 18:07:54.509: E/AndroidRuntime(13783): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784) 04-17 18:07:54.509: E/AndroidRuntime(13783): 在 android.app .ActivityThread.access$1500(ActivityThread.java:123) 04-17 18:07:54.509: E/AndroidRuntime(13783): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) 04-17 18: 07:54.509: E/AndroidRuntime(13783): 在 android.os.Handler.dispatchMessage(Handler.java:99) 04-17 18:07:54.509: E/AndroidRuntime(13783): 在 android.os.Looper.loop(Looper.java:130) 04-17 18:07:54.509: E/AndroidRuntime(13783): 在android.app.ActivityThread.main(ActivityThread.java:3835)

4

2 回答 2

0

问题是您在设置之前尝试访问“mview”。

List<Overlay> mapOverlays = mview.getOverlays(); 

将这行代码放在 setcontentview 之后

mview =(MapView)findViewById(R.id.mapv);  
于 2012-04-17T17:55:20.437 回答
0

我没有通过意图获取值,而是使用这种方式来执行它......`

The class showing list的地方

public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
          //  String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String lat = ((TextView) view.findViewById(R.id.lat)).getText().toString();
           // String description = ((TextView) view.findViewById(R.id.description)).getText().toString();
            String lng = ((TextView) view.findViewById(R.id.lng)).getText().toString();
            // Starting new intent
            Intent in = new Intent(getApplicationContext(), ViewOnMap.class);
          //  in.putExtra(KEY_NAME, name);
           // in.putExtra(KEY_DESC, description);
          //  in.putExtra(KEY_LAT, lat);
           // in.putExtra(KEY_LONG, lng);

            latitude = Double.parseDouble(lat);
            longitude = Double.parseDouble(lng);
            ViewOnMap.putLatLong(latitude,longitude);
            startActivity(in);

显示地图类中的下一个创建一个新方法来获取值

    public class ViewOnMap extends MapActivity {
private static double lat;
private static double lng;
private int latitude;
private int longitude;
MapView mview;
MapController mcontrol;
LocationManager locmanager;
Location location;
GeoPoint geopoint;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapview);
    mview =(MapView)findViewById(R.id.mapv);
    //Intent in = getIntent();
    //  String lat = in.getStringExtra(KEY_LAT);
     // String lng = in.getStringExtra(KEY_LONG);

       latitude = (int) (lat*1e6);
       longitude = (int) (lng*1e6);

      //if(in!=null)
     mview =(MapView)findViewById(R.id.mapv);
      mview.displayZoomControls(true);
      mview.setBuiltInZoomControls(true);
      mcontrol =mview.getController();
      int maxZoom = mview.getMaxZoomLevel();
      int initZoom = (int)(0.80*(double)maxZoom);
      GeoPoint point = new GeoPoint(latitude,longitude);
      mcontrol.animateTo(point); 
      mcontrol.setZoom(initZoom);


    List<Overlay> mapOverlays = mview.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
    MapOverlay itemizedoverlay = new MapOverlay(drawable, this);


     OverlayItem overlayitem = new OverlayItem(point, "some school", "some City!");

    itemizedoverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedoverlay);
}
public static void putLatLong(double latitude, double longitude){
    lat = latitude;
    lng = longitude;
}
@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}


}

`

于 2012-04-18T15:48:30.383 回答