title: Fatal Exception: java.lang.StackOverflowError: stack size 8MB on Check Location Permission Request
body: <p>I am getting an Error <code>Fatal Exception: java.lang.StackOverflowError: stack size 8M</code> while checking location permission, </p>
  
  <p>Here is the full Error Log,</p>
  
  <pre><code>   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)
  </code></pre>
  
  <p>Here is the Code I am Using to check Location Permission,</p>
  
  <p>On my Manifest file I added the following permissions,</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  &lt;uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
  </code></pre>
  
  <p>Code to check the location permission and request permission,</p>
  
  <pre><code>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;
      }
  } 
  </code></pre>