diff --git a/flutter_appauth/android/src/main/java/io/crossingthestreams/flutterappauth/FlutterAppauthPlugin.java b/flutter_appauth/android/src/main/java/io/crossingthestreams/flutterappauth/FlutterAppauthPlugin.java index 85cda7bb..a24b4894 100644 --- a/flutter_appauth/android/src/main/java/io/crossingthestreams/flutterappauth/FlutterAppauthPlugin.java +++ b/flutter_appauth/android/src/main/java/io/crossingthestreams/flutterappauth/FlutterAppauthPlugin.java @@ -60,6 +60,7 @@ public class FlutterAppauthPlugin private static final String TOKEN_ERROR_CODE = "token_failed"; private static final String END_SESSION_ERROR_CODE = "end_session_failed"; private static final String NULL_INTENT_ERROR_CODE = "null_intent"; + private static final String NULL_ACTIVITY_ERROR_CODE = "null_activity"; private static final String INVALID_CLAIMS_ERROR_CODE = "invalid_claims"; private static final String NO_BROWSER_AVAILABLE_ERROR_CODE = "no_browser_available"; @@ -74,6 +75,9 @@ public class FlutterAppauthPlugin private static final String NULL_INTENT_ERROR_FORMAT = "Failed to authorize: Null intent received"; + + private static final String NULL_ACTIVITY_ERROR_FORMAT = + "Failed to authorize: Null activity received"; private static final String NO_BROWSER_AVAILABLE_ERROR_FORMAT = "Failed to authorize: No suitable browser is available"; @@ -466,6 +470,8 @@ private void performAuthorization( authIntent, exchangeCode ? RC_AUTH_EXCHANGE_CODE : RC_AUTH); } catch (ActivityNotFoundException ex) { finishWithError(NO_BROWSER_AVAILABLE_ERROR_CODE, NO_BROWSER_AVAILABLE_ERROR_FORMAT, ex); + } catch (NullPointerException ex) { + finishWithError(NULL_ACTIVITY_ERROR_CODE, NULL_ACTIVITY_ERROR_FORMAT, ex); } } @@ -576,7 +582,12 @@ private void performEndSessionRequest( final EndSessionRequest endSessionRequest = endSessionRequestBuilder.build(); AuthorizationService authorizationService = getAuthorizationService(); Intent endSessionIntent = authorizationService.getEndSessionRequestIntent(endSessionRequest); - mainActivity.startActivityForResult(endSessionIntent, RC_END_SESSION); + + try { + mainActivity.startActivityForResult(endSessionIntent, RC_END_SESSION); + } catch (NullPointerException ex) { + finishWithError(NULL_ACTIVITY_ERROR_CODE, NULL_ACTIVITY_ERROR_FORMAT, ex); + } } private AuthorizationService getAuthorizationService() {