Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
Getting exception "java.lang.NullPointerException: Attempt to invoke
virtual method 'android.content.pm.PackageManager
android.content.Context.getPackageManager()' on a null object
reference"
when i execute below code.
Goal: change image dynamically based on some status for one flavor in android studio.
AndroidManifest.xml
<application
tools:replace="android:icon,android:roundIcon"
android:allowBackup="true"
android:icon="${appIcon}"
android:label="@string/app_name"
android:roundIcon="${appIconRound}"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="devicelist.AddDeviceActivity"/>
<activity android:name="resourcestatesimulator.NativeActivity" />
<activity android:name="devicelist.DeviceListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="CarplayMain" />
<activity-alias
android:name=".DeviceListActivityAlias"
android:enabled="false"
android:icon="@mipmap/ic_launcher_default"
android:roundIcon="@mipmap/ic_launcher_default_round"
android:label="@string/app_name"
android:targetActivity="devicelist.DeviceListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity-alias>
</application>
DeviceListActivity.java
public void defaultIcon()
Log.i(TAG, "Before default Icon" );
packageManager = getPackageManager();
packageManager.setComponentEnabledSetting(new ComponentName(DeviceListActivity.this,"com.bosch.spi.carplay.devicelist.DeviceListActivity"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
packageManager.setComponentEnabledSetting(new ComponentName(DeviceListActivity.this,"com.bosch.spi.carplay.devicelist.DeviceListActivityAlias"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Log.i(TAG, "After default Icon" );
public void activeIcon()
Log.i(TAG, "Before active Icon" );
packageManager = getPackageManager();
packageManager.setComponentEnabledSetting(new ComponentName(DeviceListActivity.this,"com.bosch.spi.carplay.devicelist.DeviceListActivity"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
packageManager.setComponentEnabledSetting(new ComponentName(DeviceListActivity.this,"com.bosch.spi.carplay.devicelist.DeviceListActivityAlias"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Log.i(TAG, "After Active Icon" );
Calling methods in DeviceListAdapter.java
DeviceListActivity deviceListActivity= new DeviceListActivity();
deviceListActivity.activeIcon();
deviceListActivity.defaultIcon();
The problem happens because you are trying to manually instantiate the Activity DeviceListActivity
. When you call getPackageManager()
inside the methods from DeviceListActivity
class it throws a NullPointerException
because your activity is not managed by Android so it don't know how to provide a package manger for it since Android runtime don't even know about your deviceListActivity
since you have instantiated it with new
keyword. Don't do this.
You have to let Android manage your Activities for you, never instantiate it with new
keyword.
Read this to understand the proper way to start an activity.
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.