2

I am developing an app that whenever you click on the get location (gpsBtn), GPS will be enabled and the Location of the user will be shown in a TextView. I get java.lang.NullPointerException error,the app has stops unexpectedly. How do I fix this?

 public class MainActivity extends AppCompatActivity {
        public LocationManager lm;
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new MyLocationListener());
            Button gpsBtn = (Button)findViewById(R.id.getLocation);
            gpsBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showCurrentLocation();
                }
            });

        public void showCurrentLocation() {
            // TODO Auto-generated method stub
            final TextView textView=(TextView)findViewById(R.id.textView);
            Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            textView.setText(String.valueOf(location.getLatitude()));

        }
        class MyLocationListener implements LocationListener {
            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }
        }

Android manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mehrdad.myapplication" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_GPS" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

</manifest>
Sualeh Fatehi
  • 4,700
  • 2
  • 24
  • 28
Emadi_mehrdad
  • 146
  • 1
  • 2
  • 16

1 Answers1

1

try.....move your final TextView textView=(TextView)findViewById(R.id.textView); to the onCreate() method. Also add textView.setText(String.valueOf(location.getLatitude())); line inside the try and catch block.

try{
 textView.setText(String.valueOf(location.getLatitude()));
}catch(Exception e){
  // exception occurs......
}

FULL CODE

public class MainActivity extends AppCompatActivity {
        public LocationManager lm;
        TextView textView;
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new MyLocationListener());
            textView = (TextView)findViewById(R.id.textView);
            Button gpsBtn = (Button)findViewById(R.id.getLocation);
            gpsBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showCurrentLocation();
                }
            });

        public void showCurrentLocation() {
            // TODO Auto-generated method stub
            Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            try{
                textView.setText(String.valueOf(location.getLatitude()));
            }catch(Exception e){
              // exception occurs......
            }
        }
        class MyLocationListener implements LocationListener {
            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }
        }
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46