在我的Android应用程序中,我只想在用户点击按钮时在地图上显示该位置。但是当我点击按钮时,android模拟器崩溃了。
以下是按钮的代码:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn_map);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
startActivity(new Intent("com337.assignment.whereami.DisplayMap"));
}
});
Log.d(tag, "In the OnCreate() event");
}
启动地图时的代码是:
public class DisplayMap extends MapActivity
{
MapView mapView;
MapController mc;
GeoPoint p;
private LocationManager lm;
private LocationListener locationListener;
protected class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//Convert the GeoPoint to screen Pixels
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//add the marker
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pushpin5);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
//mapView.setStreetView(true);
//mapView.setSatellite(true);
//navigate to a point - Belfast
mc = mapView.getController();
String coordinates[] = {"54.6000", "5.9167"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint((int) (lat*1E6), (int) (lng*1E6));
mc.animateTo(p);
mc.setZoom(13);
//add a location marker
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
//mapView.invalidate();
}
private class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc)
{
p = new GeoPoint((int)(loc.getLatitude() * 1E6), (int)(loc.getLongitude() * 1E6));
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try
{
List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6() / 1E6, p.getLongitudeE6() / 1E6, 1);
String geo = "";
if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
geo += addresses.get(0).getAddressLine(i) + "\n";
}
Toast.makeText(getBaseContext(), geo, Toast.LENGTH_SHORT).show();
}
catch (IOException e)
{
e.printStackTrace();
}
mc.animateTo(p);
mc.setZoom(18);
}
public void onProviderDisabled(String provider)
{
}
public void onProviderEnabled(String provider)
{
}
public void onStatusChanged(String provider, int status, Bundle excess)
{
}
}
//create a method so that when the user presses the volume up/down key the map zooms in/out
public boolean onKeyDown(int keyCode, KeyEvent event)
{
MapController mc = mapView.getController();
switch(keyCode)
{
case KeyEvent.KEYCODE_VOLUME_UP:mc.zoomIn();
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:mc.zoomOut();
break;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean isRouteDisplayed()
{
return false;
}
}
崩溃的更新logcat信息是:
12-13 16:40:49.231: W/dalvikvm(699): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-13 16:40:49.331: E/AndroidRuntime(699): FATAL EXCEPTION: main
12-13 16:40:49.331: E/AndroidRuntime(699): java.lang.RuntimeException: Unable to start activity ComponentInfo{com337.assignment.whereami/com337.assignment.whereami.DisplayMap}: java.lang.NullPointerException
12-13 16:40:49.331: E/AndroidRuntime(699): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-13 16:40:49.331: E/AndroidRuntime(699): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-13 16:40:49.331: E/AndroidRuntime(699): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-13 16:40:49.331: E/AndroidRuntime(699): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-13 16:40:49.331: E/AndroidRuntime(699): at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 16:40:49.331: E/AndroidRuntime(699): at android.os.Looper.loop(Looper.java:137)
12-13 16:40:49.331: E/AndroidRuntime(699): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-13 16:40:49.331: E/AndroidRuntime(699): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 16:40:49.331: E/AndroidRuntime(699): at java.lang.reflect.Method.invoke(Method.java:511)
12-13 16:40:49.331: E/AndroidRuntime(699): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-13 16:40:49.331: E/AndroidRuntime(699): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-13 16:40:49.331: E/AndroidRuntime(699): at dalvik.system.NativeStart.main(Native Method)
12-13 16:40:49.331: E/AndroidRuntime(699): Caused by: java.lang.NullPointerException
12-13 16:40:49.331: E/AndroidRuntime(699): at com337.assignment.whereami.DisplayMap.onCreate(DisplayMap.java:66)
12-13 16:40:49.331: E/AndroidRuntime(699): at android.app.Activity.performCreate(Activity.java:5008)
12-13 16:40:49.331: E/AndroidRuntime(699): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-13 16:40:49.331: E/AndroidRuntime(699): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-13 16:40:49.331: E/AndroidRuntime(699): ... 11 more
答案 0 :(得分:4)
您正在致电
startActivity(new Intent("com337.assignment.whereami.DisplayMap"));
使用new Intent("com337.assignment.whereami.DisplayMap");
意味着您要使用&#34; com337.assignment.whereami.DisplayMap&#34;的动作来创建意图。 ..由于这是您传入的类名而不是动作,因此不会找到任何活动。你没有抓住ActivityNotFoundException
,所以你会崩溃
因为你显然是在尝试启动这个课程,所以你不应该这样做..如果这是你项目的一部分你应该使用
startActivity(new Intent(YourActivityName.this, com337.assignment.whereami.DisplayMap.class));
或
startActivity(new Intent(getApplicationContext(), com337.assignment.whereami.DisplayMap.class));
您可以阅读使用意图here
答案 1 :(得分:1)
使用此构造函数:
Intent(Context packageContext, Class<?> cls)
而不是您正在使用的那个
Intent(String action)