From 26cda0dd4244bf5c5bc6dda2c343b6130f94d7d2 Mon Sep 17 00:00:00 2001 From: josephpv Date: Mon, 10 Jun 2024 18:37:03 +0000 Subject: [PATCH] Fix for settings crash during PS setup Fix for Null pointer exception to add null checks before calling unRegisterReceiver for broadcast and startActivity. Bug: 346233086 Test: Manual Change-Id: I0941c34c3f5912c57abc9497809962ed0954ac06 --- .../PrivateSpaceCreationFragment.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/com/android/settings/privatespace/PrivateSpaceCreationFragment.java b/src/com/android/settings/privatespace/PrivateSpaceCreationFragment.java index eb8864467b2..ce85d7238cc 100644 --- a/src/com/android/settings/privatespace/PrivateSpaceCreationFragment.java +++ b/src/com/android/settings/privatespace/PrivateSpaceCreationFragment.java @@ -157,22 +157,26 @@ public class PrivateSpaceCreationFragment extends InstrumentedFragment { /** Start new activity in private profile to add an account to private profile */ private void startAccountLogin() { - Intent intent = new Intent(getContext(), PrivateProfileContextHelperActivity.class); - intent.putExtra(EXTRA_ACTION_TYPE, ACCOUNT_LOGIN_ACTION); - mMetricsFeatureProvider.action( - getContext(), SettingsEnums.ACTION_PRIVATE_SPACE_SETUP_ACCOUNT_LOGIN_START); - getActivity().startActivityForResult(intent, ACCOUNT_LOGIN_ACTION); + if (isAdded() && getContext() != null && getActivity() != null) { + Intent intent = new Intent(getContext(), PrivateProfileContextHelperActivity.class); + intent.putExtra(EXTRA_ACTION_TYPE, ACCOUNT_LOGIN_ACTION); + mMetricsFeatureProvider.action( + getContext(), SettingsEnums.ACTION_PRIVATE_SPACE_SETUP_ACCOUNT_LOGIN_START); + getActivity().startActivityForResult(intent, ACCOUNT_LOGIN_ACTION); + } } private void registerReceiver() { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_PROFILE_ACCESSIBLE); - getActivity().registerReceiver(mProfileAccessReceiver, filter); + if (getContext() != null) { + getContext().registerReceiver(mProfileAccessReceiver, filter); + } } private void unRegisterReceiver() { - if (mProfileAccessReceiver != null) { - getActivity().unregisterReceiver(mProfileAccessReceiver); + if (mProfileAccessReceiver != null && isAdded() && getContext() != null) { + getContext().unregisterReceiver(mProfileAccessReceiver); } } }