Error java.lang.NoSuchMethodError: MainActivity.checkSelfPermission

0

I'm making several attempts, but I can not get a user's current location using the Location API.

I'm running the following example:

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

Location localizacao;
GoogleApiClient mapGoogleApiClient;
EditText edtLat;
EditText edtLog;
Button btLocalizacao;

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

    edtLat = (EditText) findViewById(R.id.edtLatitude);
    edtLog = (EditText) findViewById(R.id.edtLongitude);
    btLocalizacao = (Button) findViewById(R.id.btLocalizacao);

    if (mapGoogleApiClient != null) {
        mapGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    btLocalizacao.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            GPS();
        }
    });
}




@TargetApi(Build.VERSION_CODES.M)
public void GPS() {
    if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    public void requestPermissions(@NonNull String[] permissions, int requestCode)
        // 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 Activity#requestPermissions for more details.
        return;
    }
    localizacao = LocationServices.FusedLocationApi.getLastLocation(mapGoogleApiClient);
    if (localizacao != null) {
        edtLat.setText(String.valueOf(localizacao.getLatitude()));
        edtLog.setText(String.valueOf(localizacao.getLongitude()));
    } else {
        AlertDialog erroLocation = new AlertDialog.Builder(this).create();
        erroLocation.setIcon(R.drawable.alert);
        erroLocation.setTitle("Localização não encontrada");
        erroLocation.setMessage("Sua Localização não foi encontrada!! Tente novamente!");
        erroLocation.show();
    }
}

@Override
public void onConnected(Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}}

But it returns the following error:

java.lang.NoSuchMethodError: com.estudosmobileoreia.mapsdemo.MainActivity.checkSelfPermission

Knowing that AndroidManifest.xml has the following permissions:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps" >
    </activity>
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

I would like to know if someone can help me with this error or demonstrate another way to get the user's location?

My XML is correct, so there is only one Button to get the Location and 4 TextView to return the location in two of them.

    
asked by anonymous 20.08.2016 / 16:42

1 answer

1

The checkSelfPermission(String permission) method does not exist.

The method is declared in the ContextCompat class, from which ActivityCompat descends, with two parameters: checkSelfPermission (Context context, String permission) , change to:

checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
    
20.08.2016 / 16:52