androidandroid-studiolocationlistenercrash

Why App is Crashing while calling GPS?


package com.example.simranjeet.myapplication43;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class GpsLocation extends AppCompatActivity {
    Context context;
    private LocationManager locationManager;
    private LocationListener listener;


    void findloc(){
        locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

        listener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(i);
            }
        };

        configure_button();
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 10:
                configure_button();
                break;
            default:
                break;
        }
    }

    void configure_button(){
        // first check for permissions
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.INTERNET}
                        ,10);
            }
            return;
        }

        locationManager.requestLocationUpdates("gps", 500, 0, listener);
        // locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER,listener,null);
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
       Toast.makeText(context,"location finded",Toast.LENGTH_LONG ).show();
        locationManager.removeUpdates(listener);
        // Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    }
}

I'm calling findloc method from onCreate method as

s = new GpsLocation();
s.findloc();

every time i call this function it crashed but it is working well when it is not placed in seprate class. i have added every permission and it is working pretty good when it is under onCreate method can anybody tell me why it is abruptly crashing.

CRASH LOG//i am using emulator

Process: com.example.simranjeet.myapplication43, PID: 12956 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simranjeet.myapplication43/com.example.simranjeet.myapplication43.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate() at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate() at android.app.Activity.getSystemService(Activity.java:5253) at com.example.simranjeet.myapplication43.GpsLocation.findloc(GpsLocation.java:25) at com.example.simranjeet.myapplication43.MainActivity.onCreate(MainActivity.java:36) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  07-20 13:01:03.413 3353-12581/com.google.android.gms.persistent E/NetworkScheduler: Unrecognised action provided: android.intent.action.PACKAGE_REPLACED


Solution

  • Try to use.

    locationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
    

    instead

     locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);