0

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();
2

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

My requirement is to call activeIcon() and defaultIcon() in another java file.How to achieve?
But why do you need to call those methods? In order for me to be able to help, you need to provide more info. What are you trying to do? DeviceListActivity is your main activity, so in runtime, this is the first activity that should open. In what moment you want to call those methods and why?
Requirement: Change app icon based on carplay connection status for one flavor.AndroidManifest.xml and code changes are given in post. My connectionstatus logic in defined in DeviceListAdapter.java and my activity in xml is DeviceListActivity.java.
Sorry, I don't understand what you intend to do. I don't know what is "carplay connetion status" for you and what its connection to Android programming. If you really want help with whatever you want to do, you have to better communicate it. I have already explained the reason you are getting a NullPointerException and what is wrong with your code. But I don't know what your DeviceListAdapter do, where is its entry point on the application and why you are trying to instantiate DeviceListActivity from it. Without this info, I can't help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.