2

Hi i am new to android development. i am trying to get the users coordinates using the GPS, i get the error saying FATAL EXCEPTION: main Android.

here is my code

package lk.adspace.finetech;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class Main extends Activity implements LocationListener {

    TextView viewlocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,1.0f, this);
        boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER);

        if (isGPS){

        }else{
            startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
        }

        String locationProvider = LocationManager.GPS_PROVIDER;
        locationManager.requestLocationUpdates(locationProvider, 400, 1, this);

        // Location loc =   locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        Location loc =   locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        viewlocation = (TextView) findViewById(R.id.location);
        viewlocation.setText("My Current location \n Latitude = "+ loc.getLatitude()+" \n Longitude = "+ loc.getLongitude());
    }

    ...
}

This the error i get

08-20 12:08:37.436: E/AndroidRuntime(25888): FATAL EXCEPTION: main
08-20 12:08:37.436: E/AndroidRuntime(25888): java.lang.RuntimeException: Unable to start activity ComponentInfo{lk.adspace.finetech/lk.adspace.finetech.Main}: java.lang.NullPointerException
08-20 12:08:37.436: E/AndroidRuntime(25888):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
08-20 12:08:37.436: E/AndroidRuntime(25888):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
08-20 12:08:37.436: E/AndroidRuntime(25888):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
08-20 12:08:37.436: E/AndroidRuntime(25888):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
08-20 12:08:37.436: E/AndroidRuntime(25888):    at android.os.Handler.dispatchMessage(Handler.java:107)
08-20 12:08:37.436: E/AndroidRuntime(25888):    at android.os.Looper.loop(Looper.java:194)
08-20 12:08:37.436: E/AndroidRuntime(25888):    at android.app.ActivityThread.main(ActivityThread.java:5371)
08-20 12:08:37.436: E/AndroidRuntime(25888):    at java.lang.reflect.Method.invokeNative(Native Method)
08-20 12:08:37.436: E/AndroidRuntime(25888):    at java.lang.reflect.Method.invoke(Method.java:525)
08-20 12:08:37.436: E/AndroidRuntime(25888):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
08-20 12:08:37.436: E/AndroidRuntime(25888):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-20 12:08:37.436: E/AndroidRuntime(25888):    at dalvik.system.NativeStart.main(Native Method)
08-20 12:08:37.436: E/AndroidRuntime(25888): Caused by: java.lang.NullPointerException
08-20 12:08:37.436: E/AndroidRuntime(25888):    at lk.adspace.finetech.Main.onCreate(Main.java:68)
08-20 12:08:37.436: E/AndroidRuntime(25888):    at android.app.Activity.performCreate(Activity.java:5122)
08-20 12:08:37.436: E/AndroidRuntime(25888):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
08-20 12:08:37.436: E/AndroidRuntime(25888):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
08-20 12:08:37.436: E/AndroidRuntime(25888):    ... 11 more

can anyone help me to fix this problem. tnx.

Kevin Cruijssen
  • 8,426
  • 8
  • 49
  • 114
sathya
  • 23
  • 1
  • 3

3 Answers3

1

You are checking if GPS is enabled as a boolean isGPS. Move your code to within the block. Probably, the exception is thrown when the GPS provider is not enabled via settings. Your code should probably be as below:

    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,1.0f, this);
    boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER);

    if (isGPS){  
    // -------- IF GPS IS ENABLED, GET COORDINATES AND DISPLAY TO USER ------

        String locationProvider = LocationManager.GPS_PROVIDER;
        locationManager.requestLocationUpdates(locationProvider, 400, 1, this);

        Location loc =   locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (loc != null)     // <---- This line can be included to avoid null pointer exception in case you are not able to get the coordinates.
        {
            viewlocation = (TextView) findViewById(R.id.location);
             viewlocation.setText("My Current location \n Latitude = "+ loc.getLatitude()+" \n Longitude = "+ loc.getLongitude());
       }

    }else{

    // --------IF GPS NOT ENABLED, THEN MOVE TO SETTINGS SCREEN TO HELP USER TO ENABLE GPS.
        startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
    }
Nishanthi Grashia
  • 9,645
  • 5
  • 41
  • 55
0

There is null pointer exception at Line #68

8-20 12:08:37.436: E/AndroidRuntime(25888): at lk.adspace.finetech.Main.onCreate(Main.java:68)

I think loc below is returned as null. Please check.

Location loc =   locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

While accessing loc.getLatitude(), its throwing exception. You can add null check or fetch location from different provider. Also, check if GPS provider is disabled currently.

Nishanthi Grashia
  • 9,645
  • 5
  • 41
  • 55
Santosh Salunke
  • 627
  • 3
  • 10
0

I think you should check loc is null or not;

     if (loc != null)     {
     viewlocation = (TextView) findViewById(R.id.location);
     viewlocation.setText("My Current location \n Latitude = "+ loc.getLatitude()+" \n       Longitude = "+ loc.getLongitude());
     }
Ozan
  • 1,150
  • 2
  • 12
  • 31