LocationManager: java.lang.NoSuchMethodError

I am new to this community, but I have learned a lot from you! Actually, I started programming on Android Studio, but now I'm stuck somewhere. I am sure it is simple, you will quickly find out.

It works in the Nexus 5 API 23 emulator (already installed in Android Studio), but it does not start on my Android 5.1.1 API 22 mobile.

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) 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 could not find a solution on the Internet ...

Hi

+7
android android-studio
source share
1 answer

Temporary permissions were introduced in API 23 in the same way as the methods used to process them. Use the methods of the ActivityCompat class to handle this with backward compatibility.

 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.

+18
source share

All Articles