位置情報サービスの方法を学習しようとしていますが、マップ上で自分の位置をマークしようとすると、Null Pointer Exception が発生しました。この例外を削除するのを手伝ってください。
LogCat -
07-27 00:02:08.169: E/AndroidRuntime(304): FATAL EXCEPTION: main
07-27 00:02:08.169: E/AndroidRuntime(304): java.lang.NullPointerException
07-27 00:02:08.169: E/AndroidRuntime(304): at com.google.android.maps.PixelConverter.toPixels(PixelConverter.java:71)
07-27 00:02:08.169: E/AndroidRuntime(304): at com.google.android.maps.PixelConverter.toPixels(PixelConverter.java:61)
07-27 00:02:08.169: E/AndroidRuntime(304): at com.sanjay.map.Map$MyLocationOverlay.draw(Map.java:116)
07-27 00:02:08.169: E/AndroidRuntime(304): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
07-27 00:02:08.169: E/AndroidRuntime(304): at com.google.android.maps.MapView.onDraw(MapView.java:494)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.view.View.draw(View.java:6740)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.view.View.draw(View.java:6743)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.view.View.draw(View.java:6743)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-27 00:02:08.169: E/AndroidRuntime(304): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.view.ViewRoot.draw(ViewRoot.java:1407)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.os.Looper.loop(Looper.java:123)
07-27 00:02:08.169: E/AndroidRuntime(304): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-27 00:02:08.169: E/AndroidRuntime(304): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 00:02:08.169: E/AndroidRuntime(304): at java.lang.reflect.Method.invoke(Method.java:521)
07-27 00:02:08.169: E/AndroidRuntime(304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-27 00:02:08.169: E/AndroidRuntime(304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-27 00:02:08.169: E/AndroidRuntime(304): at dalvik.system.NativeStart.main(Native Method)
アクティビティ -
package com.sanjay.map;
import java.util.List;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
public class Map extends MapActivity {
MapController mapController;
MapView mapview;
GeoPoint p=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapview = (MapView)findViewById(R.id.mapview);
mapview.setBuiltInZoomControls(true);
mapController = mapview.getController();
mapController.setZoom(16);
LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
private TextView locationDetails;
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
makeUseOfNewLocation(location);
//Toast.makeText(getApplicationContext(), "New Locationdd", Toast.LENGTH_LONG).show();
}
private void makeUseOfNewLocation(Location location) {
double lon = (double) (location.getLongitude() * 1E6);
double lat = (double) (location.getLatitude() * 1E6);
int lontitue = (int)lon;
int latitute = (int)lat;
Toast.makeText(getApplicationContext(), "New Lontitue = "+ lontitue +"\n New Latitute = "+ latitute, Toast.LENGTH_LONG).show();
locationDetails = (TextView)findViewById(R.id.textViewMap);
locationDetails.setText("New Lontitue = "+ lontitue +"\n New Latitute = "+ latitute);
GeoPoint geopoint = new GeoPoint(latitute, lontitue);
mapController.animateTo(geopoint);
MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
List<Overlay> list = mapview.getOverlays();
list.add(myLocationOverlay);
//mapview.invalidate();
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public class MyLocationOverlay extends com.google.android.maps.Overlay {
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
Paint paint = new Paint();
super.draw(canvas, mapView, shadow);
// Converts lat/lng-Point to OUR coordinates on the screen.
Point myScreenCoords = new Point();
mapView.getProjection().toPixels(p, myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 255, 255);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
canvas.drawText("You are here!", myScreenCoords.x, myScreenCoords.y, paint);
return true;
}
}
}
マニフェスト -
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sanjay.map"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".Map"
android:label="@string/title_activity_map" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
レイアウト -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Waiting for location..."
android:id="@+id/textViewMap"/>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="/*My_KEY*/">
</LinearLayout>