Java 我的Android应用程序正在崩溃,don';我不理解Eclipse中的Logcat
我正在开发一个应用程序,它可以显示地图视图,并覆盖我从互联网下载的图像 该应用程序等待用户按下按钮,然后将地图置于用户当前位置的中心 一切一度都很顺利。但我想当我把我的测试手机(Galaxy SIII)升级到Jelly Bean时,事情就搞砸了 当我按下按钮获取当前GPS位置、将地图居中并覆盖图像时,应用程序崩溃 下面是LogCat的输出。我不知道这是什么意思,所以如果你能帮我理解它,我将非常感激 我认为这是问题所在的代码摘录: 按钮侦听器:Java 我的Android应用程序正在崩溃,don';我不理解Eclipse中的Logcat,java,android,eclipse,android-mapview,logcat,Java,Android,Eclipse,Android Mapview,Logcat,我正在开发一个应用程序,它可以显示地图视图,并覆盖我从互联网下载的图像 该应用程序等待用户按下按钮,然后将地图置于用户当前位置的中心 一切一度都很顺利。但我想当我把我的测试手机(Galaxy SIII)升级到Jelly Bean时,事情就搞砸了 当我按下按钮获取当前GPS位置、将地图居中并覆盖图像时,应用程序崩溃 下面是LogCat的输出。我不知道这是什么意思,所以如果你能帮我理解它,我将非常感激 我认为这是问题所在的代码摘录: 按钮侦听器: mUpdateButton.setOnClickLi
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);
};
此错误意味着在第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
}
我猜您没有初始化您的myLocationManager,但由于您没有标记哪一行是120,我无法确定 试试看。(或者最好将这一行移到onCreate方法中-您不需要每次单击按钮时都对其进行初始化。) 另一种可能性是这段代码:
(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
返回null-然后按照Sam的建议执行(检查是否为null)。
java.lang.NullPointerException位于com.androidlocation.MainActivity$1.onClick(MainActivity.java:120)
表示在第120行,您请求的变量不等于零(即null
)做某事。@Sentenial什么是行noMainActivity.java:120
?第120行是我设置LatMyLat=(int)(myLocationManager.getLastKnownLocation(LocationManager.GPS\U PROVIDER.getLatitude()*1000000)的位置代码>这是有道理的!我刚刚用我的测试手机改变了位置,现在程序可以正常工作了。因此,这一定是解决方案,手机没有得到最近的位置修复。我将在代码中实现您的解决方案,以便它检查位置是否为null。
Location lastknown = myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(lastKnown != null) {
// Read the lat / long data
}
myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); // add this line
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,myLocationListener);
(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)