gpt4 book ai didi

java - 我的 Android 应用程序崩溃了,不理解 Eclipse 中的 Logcat

转载 作者:行者123 更新时间:2023-11-30 09:27:40 25 4
gpt4 key购买 nike

我正在开发一个显示 map View 并覆盖从互联网下载的图像的应用。

应用等待用户按下按钮,然后将 map 以用户当前位置为中心。

一度一切正常。但是当我将我的测试手机 (Galaxy SIII) 更新到 Jelly Bean 时,我认为事情变得一团糟。

当我按下按钮以获取当前 GPS 位置、将 map 居中并覆盖图像时,应用程序崩溃。

下面是 LogCat 的输出。我不确定其中任何一个是什么意思,所以如果你能帮助我理解它,我将不胜感激!

我认为是问题所在的代码摘录:

按钮监听器:

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

最佳答案

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
}

关于java - 我的 Android 应用程序崩溃了,不理解 Eclipse 中的 Logcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14465795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com