ユーザーがボタンをクリックしたときに、単に場所を地図上に表示したい私の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