When I use this permission an error occurs in ALL. What do I do? [closed]

0

When I use this permission, an error occurs in TODO. What do 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 error:

  

Error: (40, 20) error: incompatible types: unexpected return value

    
asked by anonymous 12.12.2016 / 04:46

2 answers

5

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

In addition to verifying that the permission exists, it is necessary to request, if it does not exist!

Here's 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);
        }
    }
}
    
12.12.2016 / 12:58
3

Notice this:

return TODO;

Who is TODO ? What animal is that? What do you eat? Where do you live?

The answer is that TODO comes from English to do which means " to do ". 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 did not put anything in and left TODO 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 compilation error.

In addition, 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 to 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.
    
12.12.2016 / 13:53