我正在开发一个显示地图视图的应用,并覆盖我从互联网上下载的图片。
应用等待用户按下按钮,然后将地图置于用户当前位置的中心位置。
一切都很好。但是当我将测试手机(Galaxy SIII)更新为Jelly Bean时,我觉得事情搞砸了。
当我按下按钮获取当前GPS位置,使地图居中并覆盖图像时,应用程序崩溃。
以下是LogCat的输出。我不确定它是什么意思,所以如果你能帮助我理解它,我会非常感激!
从我的代码中摘录我认为是问题:
Button监听器:
mUpdateButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
// Start listening for location
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,myLocationListener);
// Get Lat and Long coordinates, and format them
MyLat = (int)(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()*1000000);
MyLong = (int)(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude()*1000000);
// Save the Lat and Long into a GeoPoint
GeoPoint initGeoPoint = new GeoPoint(MyLat,MyLong);
// Center location on current pos
CenterLocatio(initGeoPoint, 10);
// Stop listening for location
myLocationManager.removeUpdates(myLocationListener);
}
});
CenterLocatio功能:
private void CenterLocatio(GeoPoint centerGeoPoint, int zoomLevel)
{
myMapController.animateTo(centerGeoPoint);
myMapController.setZoom(zoomLevel);
myLongitude.setText("Longitude: "+
String.valueOf((float)centerGeoPoint.getLongitudeE6()/1000000)
);
myLatitude.setText("Latitude: "+
String.valueOf((float)centerGeoPoint.getLatitudeE6()/1000000)
);
// Get location of the storage location
File extStore = Environment.getExternalStorageDirectory();
String PATH = extStore + "/data/tntechpie/";
// Add new overlay to Map
List<Overlay> mapOverlays = myMapView.getOverlays();
// Set drawable to the downloaded image from the storage location
//Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
@SuppressWarnings("deprecation")
Drawable drawable = new BitmapDrawable(PATH + "testMap.gif");
myItemizedOverlay itemizedoverlay = new myItemizedOverlay(drawable, this);
OverlayItem overlayitem = new OverlayItem(centerGeoPoint, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
};
LogCat输出:
01-22 12:34:31.062: I/MapActivity(6919): Handling network change notification:CONNECTED
01-22 12:34:31.062: E/MapActivity(6919): Couldn't get connection factory client
01-22 12:34:33.465: D/AndroidRuntime(6919): Shutting down VM
01-22 12:34:33.465: W/dalvikvm(6919): threadid=1: thread exiting with uncaught exception (group=0x41f44438)
01-22 12:34:33.475: E/AndroidRuntime(6919): FATAL EXCEPTION: main
01-22 12:34:33.475: E/AndroidRuntime(6919): java.lang.NullPointerException
01-22 12:34:33.475: E/AndroidRuntime(6919): at com.androidlocation.MainActivity$1.onClick(MainActivity.java:120)
01-22 12:34:33.475: E/AndroidRuntime(6919): at android.view.View.performClick(View.java:4198)
01-22 12:34:33.475: E/AndroidRuntime(6919): at android.view.View$PerformClick.run(View.java:17158)
01-22 12:34:33.475: E/AndroidRuntime(6919): at android.os.Handler.handleCallback(Handler.java:615)
01-22 12:34:33.475: E/AndroidRuntime(6919): at android.os.Handler.dispatchMessage(Handler.java:92)
01-22 12:34:33.475: E/AndroidRuntime(6919): at android.os.Looper.loop(Looper.java:137)
01-22 12:34:33.475: E/AndroidRuntime(6919): at android.app.ActivityThread.main(ActivityThread.java:4918)
01-22 12:34:33.475: E/AndroidRuntime(6919): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 12:34:33.475: E/AndroidRuntime(6919): at java.lang.reflect.Method.invoke(Method.java:511)
01-22 12:34:33.475: E/AndroidRuntime(6919): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
01-22 12:34:33.475: E/AndroidRuntime(6919): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
01-22 12:34:33.475: E/AndroidRuntime(6919): at dalvik.system.NativeStart.main(Native Method)
01-22 12:34:41.924: I/Process(6919): Sending signal. PID: 6919 SIG: 9
答案 0 :(得分:2)
java.lang.NullPointerException
at com.androidlocation.MainActivity$1.onClick(MainActivity.java:120)
这个错误意味着在第120行,你要求一个等于什么的变量(即null
)来做某事。问题可能发生在onClick()
方法中:
MyLat = (int)(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()*1000000);
MyLong = (int)(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude()*1000000);
但如果最近没有可用修补程序,则getLastKnownLocation()
可能会返回null
。在尝试从中读取之前,您应该检查它是否返回可用值。
Location lastknown = myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(lastKnown != null) {
// Read the lat / long data
}
答案 1 :(得分:0)
我猜你没有初始化 myLocationManager ,但由于你没有标记哪一行是120,我无法确定。
试试这样。 (或者更好地移动此行而不是onCreate方法 - 每次单击按钮时都不需要初始化它。)
myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); // add this line
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,myLocationListener);
另一种可能性是这段代码:
(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
返回null - 比Sam建议的那样(检查它是否为空)。