0

My application crashes on launch because of missing permissions.

I already added these missing permissions in manifest.xml and also implemented run time permissions inside the class.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

This my logcat. It was just working when I opened it last time and I didn't change anything.

What can be the problem?

E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.example.escaper.smoosh4340, PID: 29873
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.escaper.smoosh4340/com.example.escaper.smoosh4340.LocationWeather}: java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6119)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
    Caused by: java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
       at android.os.Parcel.readException(Parcel.java:1684)
       at android.os.Parcel.readException(Parcel.java:1637)
       at android.location.ILocationManager$Stub$Proxy.getLastLocation(ILocationManager.java:725)
       at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1205)
       at com.example.escaper.smoosh4340.LocationWeather.onCreate(LocationWeather.java:64)
       at android.app.Activity.performCreate(Activity.java:6679)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)

this is my java class

  private static final String APP_ID="1c163fcac8ac8ca256fd3119777bee59";
    Button button;
    TextView textView;
    public static final int MY_PERMISSION_REQUEST_LOCATION = 1;

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

        button = (Button)findViewById(R.id.btnGetlc);

        textView = (TextView)findViewById(R.id.textlc);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ContextCompat.checkSelfPermission(LocationWeather.this,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
                    if(ActivityCompat.shouldShowRequestPermissionRationale(LocationWeather.this,
                            Manifest.permission.ACCESS_COARSE_LOCATION)){
                        ActivityCompat.requestPermissions(LocationWeather.this,
                                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSION_REQUEST_LOCATION);

                    }else {
                        ActivityCompat.requestPermissions(LocationWeather.this,
                                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSION_REQUEST_LOCATION);
                    }
                }else {
                    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                    Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    try{
                        textView.setText(yourLocation(location.getLatitude(), location.getLongitude()));
                    }catch (Exception e){
                        e.printStackTrace();
                        Toast.makeText(LocationWeather.this, "NOT FOUND, TURN ON GPS", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);









        String units = "metric";
        String url = String.format("http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&units=%s&appid=%s",
                location.getLatitude(), location.getLongitude(), units,APP_ID);

        TextView textView = (TextView) findViewById(R.id.textweat);
        new WeatherDetection(textView).execute(url);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode,  String[] permissions,  int[] grantResults) {
        switch (requestCode){
            case MY_PERMISSION_REQUEST_LOCATION:{
                if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    if(ContextCompat.checkSelfPermission(LocationWeather.this,
                            Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
                        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        try{
                            textView.setText(yourLocation(location.getLatitude(), location.getLongitude()));
                        }catch (Exception e){
                            e.printStackTrace();
                            Toast.makeText(LocationWeather.this, "NOT FOUND, TURN ON GPS", Toast.LENGTH_SHORT).show();
                        }
                    }
                }else{
                    Toast.makeText(LocationWeather.this, "No permission!", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
noman404
  • 928
  • 1
  • 8
  • 23
Escaper
  • 69
  • 1
  • 9
  • On what Android version are you running this? And please post the code for getting the location (using LocationManager.NETWORK_PROVIDER I assume). – Gino Mempin Apr 05 '18 at 00:06
  • you are using the locationManager before giving the permission. Remove the two line below the button.setOnclicklistener method. And also get the `android.permission.ACCESS_FINE_LOCATION` permission. – Nishikanto Sarkar Simul Apr 05 '18 at 00:51
  • Refer to this https://stackoverflow.com/questions/32083913/android-gps-requires-access-fine-location-error-even-though-my-manifest-file – Noah Apr 05 '18 at 04:08

2 Answers2

0

Look at your Exception

Caused by: java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.

It says that you have missed the permission of

android.permission.ACCESS_FINE_LOCATION

Add that permission to menifest.xml as well as your class code.

noman404
  • 928
  • 1
  • 8
  • 23
0

You are not checking your permissions before calling:

locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

You only check the permission after you click on the button, so that's too late.

njzk2
  • 38,969
  • 7
  • 69
  • 107