2

我正在开发一个显示地图视图的应用程序,并覆盖我从互联网下载的图像。

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

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

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

以下是 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);
        }
    });

中心位置功能:

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
4

2 回答 2

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
}
于 2013-01-22T18:51:29.110 回答
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 建议的(检查它是否不为 null)。

于 2013-01-22T19:16:04.363 回答