I’m trying to open mobile data settings on Android API 33 and for that to happen, from an activity I run the following code:
Intent roamingIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
try {
startActivity(roamingIntent);
} catch (Exception e) {
// failed
}
It works fine on many brands of Android phones that I have tested, but when I tested on Google Pixel 5, Google Pixel 6 and Google Pixel 7 the mobile data settings page is not opened and I get the following error:
Exception java.lang.RuntimeException: Unable to resume activity {com.android.settings/com.android.settings.Settings$MobileNetworkActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.telephony.TelephonyManager.isDataRoamingEnabled()' on a null object reference
at android.app.ActivityThread.performResumeActivity (ActivityThread.java:4803)
at android.app.ActivityThread.handleResumeActivity (ActivityThread.java:4836)
at android.app.servertransaction.ResumeActivityItem.execute (ResumeActivityItem.java:54)
at android.app.servertransaction.ActivityTransactionItem.execute (ActivityTransactionItem.java:45)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState (TransactionExecutor.java:176)
at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2308)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loopOnce (Looper.java:201)
at android.os.Looper.loop (Looper.java:288)
at android.app.ActivityThread.main (ActivityThread.java:7898)
at java.lang.reflect.Method.invoke
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:936)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.telephony.TelephonyManager.isDataRoamingEnabled()' on a null object reference
at com.android.settings.network.telephony.RoamingPreferenceController.isChecked (RoamingPreferenceController.java:151)
at com.android.settings.core.TogglePreferenceController.updateState (TogglePreferenceController.java:69)
at com.android.settings.network.telephony.RoamingPreferenceController.updateState (RoamingPreferenceController.java:127)
at com.android.settings.network.telephony.AbstractMobileNetworkSettings.updateVisiblePreferenceControllers (AbstractMobileNetworkSettings.java:130)
at com.android.settings.network.telephony.AbstractMobileNetworkSettings.lambda$updatePreferenceStates$3 (AbstractMobileNetworkSettings.java:109)
at com.android.settings.network.telephony.AbstractMobileNetworkSettings.$r8$lambda$UO4kIAXRZT9w_Q2HJ-kfkIYs2C8
at com.android.settings.network.telephony.AbstractMobileNetworkSettings$$ExternalSyntheticLambda3.accept
at java.util.ArrayList.forEach (ArrayList.java:1262)
at com.android.settings.network.telephony.AbstractMobileNetworkSettings.updatePreferenceStates (AbstractMobileNetworkSettings.java:108)
at com.android.settings.dashboard.DashboardFragment.onResume (DashboardFragment.java:226)
at com.android.settings.dashboard.RestrictedDashboardFragment.onResume (RestrictedDashboardFragment.java:138)
at com.android.settings.network.telephony.MobileNetworkSettings.onResume (MobileNetworkSettings.java:256)
at androidx.fragment.app.Fragment.performResume (Fragment.java:3037)
at androidx.fragment.app.FragmentStateManager.resume (FragmentStateManager.java:586)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState (FragmentStateManager.java:283)
at androidx.fragment.app.FragmentStore.moveToExpectedState (FragmentStore.java:113)
at androidx.fragment.app.FragmentManager.moveToState (FragmentManager.java:1331)
at androidx.fragment.app.FragmentManager.dispatchStateChange (FragmentManager.java:2772)
at androidx.fragment.app.FragmentManager.dispatchResume (FragmentManager.java:2729)
at androidx.fragment.app.FragmentController.dispatchResume (FragmentController.java:284)
at androidx.fragment.app.FragmentActivity.onResumeFragments (FragmentActivity.java:434)
at androidx.fragment.app.FragmentActivity.onPostResume (FragmentActivity.java:423)
at android.app.Activity.performResume (Activity.java:8447)
at android.app.ActivityThread.performResumeActivity (ActivityThread.java:4793)
Documentation:
public static final String ACTION_DATA_ROAMING_SETTINGS
Activity Action: Show settings for selection of 2G/3G.
In some cases, a matching Activity may not exist, so ensure you safeguard against this.
Input: Nothing.
Output: Nothing.
I catch the exception if that is what they mean with “safe guard”, or should I run some intent matcher to check if any app handles the intent first? However, it still would not help in showing the data settings page.