致命异常:java.lang.StackOverflowError:检查位置权限请求上的堆栈大小为8MB

时间:2018-10-07 10:33:19

标签: android location android-permissions

检查位置权限时出现错误Fatal Exception: java.lang.StackOverflowError: stack size 8M

这是完整的错误日志,

   Fatal Exception: java.lang.StackOverflowError: stack size 8MB
   at java.lang.String.contains(String.java:2078)
   at android.os.BinderProxy.isMsgForGoogleLocation(Binder.java:718)
   at android.os.BinderProxy.transact(Binder.java:625)
   at android.app.ActivityManagerProxy.checkPermission(ActivityManagerNative.java:5684)
   at android.app.ContextImpl.checkPermission(ContextImpl.java:1644)
   at android.content.ContextWrapper.checkPermission(ContextWrapper.java:721)
   at android.support.v4.content.ContextCompat.checkSelfPermission(ContextCompat.java:439)
   at com.bmates.app.HomeActivity.checkLocationPermission(HomeActivity.java:258)
   at com.bmates.app.HomeActivity.onRequestPermissionsResult(HomeActivity.java:270)
   at android.app.Activity.requestPermissions(Activity.java:4291)
   at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
   at com.bmates.app.HomeActivity.checkLocationPermission(HomeActivity.java:261)
   at com.bmates.app.HomeActivity.onRequestPermissionsResult(HomeActivity.java:270)
   at android.app.Activity.requestPermissions(Activity.java:4291)
   at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
   at com.bmates.app.HomeActivity.checkLocationPermission(HomeActivity.java:261)
   at com.bmates.app.HomeActivity.onRequestPermissionsResult(HomeActivity.java:270)
   at android.app.Activity.requestPermissions(Activity.java:4291)
   at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
   at com.bmates.app.HomeActivity.checkLocationPermission(HomeActivity.java:261)
   at com.bmates.app.HomeActivity.onRequestPermissionsResult(HomeActivity.java:270)
   at android.app.Activity.requestPermissions(Activity.java:4291)
   at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
   at com.bmates.app.HomeActivity.checkLocationPermission(HomeActivity.java:261)
   at com.bmates.app.HomeActivity.onRequestPermissionsResult(HomeActivity.java:270)
   at android.app.Activity.requestPermissions(Activity.java:4291)
   at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
   at com.bmates.app.HomeActivity.checkLocationPermission(HomeActivity.java:261)
   at com.bmates.app.HomeActivity.onRequestPermissionsResult(HomeActivity.java:270)
   at android.app.Activity.requestPermissions(Activity.java:4291)
   at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
   at com.bmates.app.HomeActivity.checkLocationPermission(HomeActivity.java:261)
   at com.bmates.app.HomeActivity.onRequestPermissionsResult(HomeActivity.java:270)
   at android.app.Activity.requestPermissions(Activity.java:4291)
   at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
   at com.bmates.app.HomeActivity.checkLocationPermission(HomeActivity.java:261)
   at com.bmates.app.HomeActivity.onRequestPermissionsResult(HomeActivity.java:270)
   at android.app.Activity.requestPermissions(Activity.java:4291)
   at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
   at com.bmates.app.HomeActivity.checkLocationPermission(HomeActivity.java:261)
   at com.bmates.app.HomeActivity.onRequestPermissionsResult(HomeActivity.java:270)
   at android.app.Activity.requestPermissions(Activity.java:4291)
   at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
   at com.bmates.app.HomeActivity.checkLocationPermission(HomeActivity.java:261)
   at com.bmates.app.HomeActivity.onRequestPermissionsResult(HomeActivity.java:270)
   at android.app.Activity.requestPermissions(Activity.java:4291)
   at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
   at com.bmates.app.HomeActivity.checkLocationPermission(HomeActivity.java:261)
   at com.bmates.app.HomeActivity.onRequestPermissionsResult(HomeActivity.java:270)
   at android.app.Activity.requestPermissions(Activity.java:4291)
   at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
   at com.bmates.app.fragment.HomeFragment.checkLocationPermission(HomeFragment.java:211)
   at com.bmates.app.fragment.HomeFragment.onResume(HomeFragment.java:123)
   at android.support.v4.app.Fragment.performResume(Fragment.java:2390)
   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1474)
   at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
   at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3244)
   at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:3212)
   at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:217)
   at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:509)
   at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:498)
   at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:171)
   at android.app.Activity.performResume(Activity.java:7112)
   at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3768)
   at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3832)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2994)
   at android.app.ActivityThread.-wrap14(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6682)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

这是我用来检查位置权限的代码,

在清单文件上,我添加了以下权限,

<uses-permission android:name="android.permission.INTERNET" />
<permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

用于检查位置权限和请求权限的代码,

private boolean checkLocationPermission() {
        boolean permission = false;
        if (ActivityCompat.checkSelfPermission(HomeActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            permission = true;
        } else
            ActivityCompat.requestPermissions(HomeActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, Constants.LOCATION_REQUEST_CODE);
        return permission;
    }


@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case Constants.LOCATION_REQUEST_CODE:
            checkLocationPermission();
            break;
    }
} 

1 个答案:

答案 0 :(得分:1)

您的代码说您一次又一次地获得许可,直到给予stackOverFlow

要解决此问题,请改用此代码

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
    case Constants.LOCATION_REQUEST_CODE:
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // permission granted
       }else{
            //show some warning
       }
        break;
     }
}