LocationManager:java.lang.NoSuchMethodError

我是这个社区的新人,但我已经习惯了从你那里学到很多东西! 其实我开始在Android Studio上编程,但现在我卡在某个地方。 我相信这是简单的,你会发现很快。

在模拟器Nexus 5 API 23(Android Studio中已经安装的那个)中,它正在工作,但是在我的移动Android 5.1.1 API 22中,它开始运行。

错误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代码:

 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) { } } 

对不起,不好的post。 顺便说一句:我不能find解决scheme在networking上…

问候

Solutions Collecting From Web of "LocationManager:java.lang.NoSuchMethodError"

API 23中引入了运行时权限,与用于处理的方法相同。 使用ActivityCompat类方法以向后兼容的方式处理这个问题。

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

等等。