0

I have released my Android app but I am seeing a lot of the following errors from users. I cannot reproduce the issue myself every time on the emulator so it's hard for me to debug it myself.

It appears to happen when the user opens my app after a while of being in the background. App will crash on first run. When the user opens the app again everything is fine. I feel it is something to do with either my permission or context being null. I do not have too much experience reading nullpointerexeptions so would love some help!

Error:

java.lang.NullPointerException: 
  at android.support.v4.content.ContextCompat.checkSelfPermission (ContextCompat.java:425)
  at me.XXXXXXX.XXXXXXXX.DistanceFragment.getDeviceLocation (DistanceFragment.java:463)
  at me.XXXXXXX.XXXXXXXX.DistanceFragment.onConnected (DistanceFragment.java:239)
  at com.google.android.gms.common.internal.zzm.zzq (Unknown Source)
  at com.google.android.gms.internal.zzaal.zzo (Unknown Source)
  at com.google.android.gms.internal.zzaaj.zzvE (Unknown Source)
  at com.google.android.gms.internal.zzaaj.onConnected (Unknown Source)
  at com.google.android.gms.internal.zzaan.onConnected (Unknown Source)
  at com.google.android.gms.internal.zzzy.onConnected (Unknown Source)
  at com.google.android.gms.common.internal.zzl$1.onConnected (Unknown Source)
  at com.google.android.gms.common.internal.zzf$zzj.zzwZ (Unknown Source)
  at com.google.android.gms.common.internal.zzf$zza.zzc (Unknown Source)
  at com.google.android.gms.common.internal.zzf$zza.zzu (Unknown Source)
  at com.google.android.gms.common.internal.zzf$zze.zzxa (Unknown Source)
  at com.google.android.gms.common.internal.zzf$zzd.handleMessage (Unknown Source)
  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 (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1520)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1410)

Am I right in saying that...

DistanceFragment.onConnected (DistanceFragment.java:239) is called first
---> then calls DistanceFragment.getDeviceLocation (DistanceFragment.java:463)
------> checkSelfPermission (ContextCompat.java:425)*

Check permission has 2 parameters:

context Context permission String:

One of these is null?

Code below for these lines:

/// Line 239

@Override
public void onConnected(Bundle connectionHint) {

    getDeviceLocation(); /// Line 239

/// Line 463

   if (ContextCompat.checkSelfPermission(getContext(),
                android.Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mLocationPermissionGranted = true;
        } else {
            ActivityCompat.requestPermissions(getActivity(),
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
        }

Thanks for your help.

If anyone had a solution on how to ensure this is not null, it would be greatly appreciated.

Nakatomi
  • 104
  • 2
  • 13

1 Answers1

5

Am I right in saying that... One of these is null?

Yes.

If anyone had a solution on how to ensure this is not null, it would be greatly appreciated.

Fix your fragment. Only be registered with Play Services while the fragment is attached to an activity. Unregister from Play Services at other times, such as when the fragment is detached. Right now, your onConnected() callback is being called sometime when the fragment is not attached to an activity, and so getActivity() is returning null.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Ok, firstly thank you. Currently my onConnected() is like this...https://codepaste.net/edy4kz should I move getDeviceLocation(); under SupportMapFragment mapFragment ? – Nakatomi Jul 27 '17 at 11:27
  • @Malorrr: That will not address this problem. – CommonsWare Jul 27 '17 at 11:28
  • I have changed this to getContext() previously and this did not solve the issue either. I'm puzzled to how this method is called without being attached to a fragment? – Nakatomi Jul 27 '17 at 11:31
  • @Malorrr: Where are you calling `connect()` and where are you calling `disconnect()` on the `GoogleApiClient`? – CommonsWare Jul 27 '17 at 11:36
  • I Connect in OnCreateView() of Fragment And I don't appear to ever dissconnect......... – Nakatomi Jul 27 '17 at 12:16
  • @Malorrr: That is a problem in general. So, any time this fragment gets detached from its hosting activity (configuration change, navigation, etc.), it is still eligible to be called with `onConnect()` (and you may have a memory leak). For example, you might be able to reproduce your problem simply by rotating the screen (e.g., from portrait to landscape). – CommonsWare Jul 27 '17 at 12:22
  • @Malorrr: I have not used that. The method signature takes a `FragmentActivity`, and you are attempting to connect from inside of a fragment. – CommonsWare Jul 27 '17 at 12:26
  • Yes, I can see that from this. https://stackoverflow.com/questions/30622906/using-enableautomanage-in-fragment I will try later. Thank you for your help here. – Nakatomi Jul 27 '17 at 12:27