Sentenial
Sentenial

Reputation: 61

My Android app is crashing, don't understand Logcat in Eclipse

I am developing an app that displays a mapview, and overlays an image that I download from the internet.

The app waits for the user to press a button, and then centers the map on the user's current location.

Everything was working fine at one point. But I think things got messed up when I updated my test phone ( Galaxy SIII ) to Jelly Bean.

The app crashes when I press the button to get the current GPS position, center the map, and overlay the image.

Below is the output from LogCat. I am unsure what any of it means, so if you could help me make sense of it I would really appreciate it!

Excerpt from my code that I think is the issue:

The Button listener:

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);
        }
    });

The CenterLocatio function:

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);
};

The LogCat Output:

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

Upvotes: 2

Views: 695

Answers (2)

Andrzej Gis
Andrzej Gis

Reputation: 14316

I guess you didn't initialize your myLocationManager but as you didn't mark which line is 120 I cannot be sure.

Try like that. (or better move this line no your onCreate method - you don't need to initialize it every time the button is clicked.)

myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); // add this line
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,myLocationListener); 

The other possibility is that this piece of code:

(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)

returns null - than do as Sam suggested (check if it's not null).

Upvotes: 0

Sam
Sam

Reputation: 86958

java.lang.NullPointerException 
    at com.androidlocation.MainActivity$1.onClick(MainActivity.java:120) 

This error means that on line 120, you are asking a variable that equal nothing (ie null) to do something. The problem probably happens here in your onClick() method:

MyLat = (int)(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()*1000000);
MyLong = (int)(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude()*1000000);

But getLastKnownLocation() may return null if there is no recent fix available. You should check to see if this returns a usable value before trying to read from it.

Location lastknown = myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(lastKnown != null) {
    // Read the lat / long data
}

Upvotes: 2

Related Questions