Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocationManager: java.lang.NoSuchMethodError

I'm new in this community but I already used to learn much from you! Actually I started programming on Android Studio but now I stuck somewhere. I'm sure it's something simple you will find out quick.

In the Emulator Nexus 5 API 23 (the one already installed in Android Studio) it's working but on my mobile Android 5.1.1 API 22 it doensnt start.

Error: 09-20 19:15:11.526 17776-17776/com.example.stefano.aiutomamma01 E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.stefano.aiutomamma01, PID: 17776 java.lang.NoSuchMethodError: No virtual method checkSelfPermission(Ljava/lang/String;)I in class Lcom/example/stefano/aiutomamma01/MainActivity; or its super classes (declaration of 'com.example.stefano.aiutomamma01.MainActivity' appears in /data/app/com.example.stefano.aiutomamma01-2/base.apk) at com.example.stefano.aiutomamma01.MainActivity.onCreate(MainActivity.java:28) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) at android.app.ActivityThread.access$800(ActivityThread.java:156) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:5373) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

JAVA Code:

package com.example.stefano.aiutomamma01;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements LocationListener {

    //GPS
    private LocationManager locationManager;

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

        //GPS
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //CATCHED
        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;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    //GPS
    @Override
    public void onLocationChanged(Location location) {

        String str = "Latitude: " + location.getLatitude() + "Longitude: " + location.getLongitude();

        Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();

    }

    //GPS
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    //GPS
    @Override
    public void onProviderEnabled(String provider) {

    }

    //GPS
    @Override
    public void onProviderDisabled(String provider) {

    }

}

Sorry for the bad post. Btw: I couldnt find solution in the web...

Greetings

like image 285
Ero Stefano Avatar asked Sep 20 '15 17:09

Ero Stefano


1 Answers

Runtime permissions were introduced in API 23 same as the methods which are used for their handling. Use the ActivityCompat class methods to handle this in backwards compatible fashion.

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
    && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    ...
}

And so on.

like image 129
Eugen Pechanec Avatar answered Oct 13 '22 07:10

Eugen Pechanec