When I use this permission an error occurs in the WHOLE. What do I do?

Asked

Viewed 93 times

0

When I use this permission an error occurs in the WHOLE. What I do?

 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return TODO;
            }

This is the mistake:

Error:(40, 20) error: incompatible types: Unexpected Return value

  • Have to return something then. What would be ALL?

  • Bro, you know what this permit is for?

  • this ALL it creates automatically

  • Localization. But your doubt doesn’t seem to be about it. You don’t seem to understand what you’re doing. You copied and pasted?

  • I really don’t know. I am trying to make a project q returns the current latitude and longitude and when I use Locationmanager.requestLocationUpdate() it asks for this permission.

  • Look here on the site about asking for permissions on Android 6. You have to understand what you’re doing before asking why the code doesn’t work.

Show 1 more comment

2 answers

5


The error occurs because it is returning a variable that does not exist !

In addition to checking if there is permission it is necessary to request, if there is no!

Follow an example:

public static final void checarPermissao(final Activity activity) {
    if(null == activity) return;

    // VErifica se você tem uma permissão específica.
    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        // Obtém se você deve mostrar  a UI com justificativa para solicitar uma permissão.
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {

            Toast.makeText(activity, "Por favor, permita o acesso a localização", Toast.LENGTH_SHORT).show();
            ActivityCompat.requestPermissions(activity,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    CODE_PERMISSION_LOCATION);
        } else {
            // Chama a Activity que irá solicitar a permissão ao usuário
            ActivityCompat.requestPermissions(activity,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    CODE_PERMISSION_LOCATION);
        }
    }
}

3

Watch this:

return TODO;

Who is TODO? What is this? What do you eat? Where do you live?

The answer is that TODO comes from the English to do which means "to make". That is, this is something that was automatically generated to say that the code is incomplete and that you have to put something there.

However you put nothing and left the TODO just the way it was. The compiler will try to see this as a variable name, but this variable does not exist, and therefore you will take a build error.

Also, see the comment that precedes this:

            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.

Translating into Portuguese:

            // A FAZER: Considere chamar
            //    ActivityCompat#requestPermissions
            // aqui para requisitar as permissões que estiverem faltando, e então sobreescrever
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // para tratar o caso onde o usuário concede a permissão. Veja a documentação
            // de ActivityCompat#requestPermissions para mais detalhes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.