Karan Nayyar
Karan Nayyar

Reputation: 57

Operator '==' cannot be applied to 'java.lang.String', 'int error

I'm setting dexter library functions in my android application code.This loop was to check for the location services, if they are on so to start the map activity.

if (ContextCompat.checkSelfPermission(depressionHelpline.this, Manifest.permission.ACCESS_FINE_LOCATION == PackageManager.PERMISSION_GRANTED)) {
            startActivity(new Intent(depressionHelpline.this,MapActivity.class));
            finish();
            return;
        }

I expect it to work normally but it shows this error

Operator '==' cannot be applied to 'java.lang.String', 'int' Condition 'Manifest.permission.ACCESS_FINE_LOCATION == PackageManager.PERMISSION_GRANTED' is always 'false' less... (Ctrl+F1) Inspection info: This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant, and situations that can lead to null ability contract violations. Variables, method parameters and return values marked as @Nullable or @NotNull are treated as null able (or not-null, respectively) and used during the analysis to check null ability contracts, e.g. report NullPointerException (NPE) errors that might be produced. More complex contracts can be defined using @Contract annotation, for example: @Contract("_, null -> null") — method returns null if its second argument is null @Contract("_, null -> null; _, !null -> !null") — method returns null if its second argument is null and not-null otherwise @Contract("true -> fail") — a typical assert False method which throws an exception if true is passed to it The inspection can be configured to use custom @Nullable @NotNull annotations (by default the ones from annotations.jar will be used)strong text

Upvotes: 0

Views: 1998

Answers (2)

Avaneesh Maurya
Avaneesh Maurya

Reputation: 11

If you go inside Manifest.permission.ACCESS_FINE_LOCATION then you can see the actual value is string :-

public static final String ACCESS_FINE_LOCATION = "android.permission.ACCESS_FINE_LOCATION";

And PackageManager.PERMISSION_GRANTED is returning an integer value:-

public static final int PERMISSION_GRANTED = 0;

Manifest.permission.ACCESS_FINE_LOCATION this is a string and PackageManager.PERMISSION_GRANTED is an integer then how can we compare String and Int. The right way to do is :-

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
//            Permission is granted. do your work here
        } else {
//            Permission is not granted. Request permission
        }

Upvotes: 0

Prachi Singh
Prachi Singh

Reputation: 582

Its just a bracket problem.. Replace your line with this line.

if (ContextCompat.checkSelfPermission(depressionHelpline.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

Upvotes: 5

Related Questions