Wisam Atil
Wisam Atil

Reputation: 91

Error Attempt to invoke virtual method. on a null object reference

this doesn't work after doing project cleanup and code cleanup im getting the following error Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setCustomView(android.view.View, androidx.appcompat.app.ActionBar$LayoutParams)' on a null object reference

final ActionBar abar = getSupportActionBar();
    //line under the action bar
    View viewActionBar = getLayoutInflater().inflate(R.layout.app_bar_layout, null);
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.MATCH_PARENT,
            Gravity.CENTER);
    TextView textviewTitle = (TextView) viewActionBar.findViewById(R.id.actionbar_textview);
    textviewTitle.setText("WizChat");
    assert abar != null;
    abar.setCustomView(viewActionBar, params);
    abar.setDisplayShowCustomEnabled(true);
    abar.setDisplayShowTitleEnabled(false);
    abar.setDisplayHomeAsUpEnabled(true);
    abar.setIcon(R.color.background_color);
    abar.setHomeButtonEnabled(true);

you can see here the code this is my manifest file as you requested

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.logindesign"
    android:versionCode="1"
    android:versionName="1.0" >
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.logindesign.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.logindesign.Welcome"/>
    </application>
</manifest>

you can see here the code this is my style file as you requested

<resources>
    <style name="AppBaseTheme" parent="@style/Theme.AppCompat.NoActionBar">
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
    </style>
</resources>

Upvotes: 0

Views: 544

Answers (2)

FlashspeedIfe
FlashspeedIfe

Reputation: 378

In your styles.xml file change

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.NoActionBar">

to

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">

Your current AppBaseTheme is using a theme with no ActionBar so your call to .getSupportActionBar() returns null because your theme does not have an action bar.

Upvotes: 0

Afsar Ahamad
Afsar Ahamad

Reputation: 1997

You are using Theme.AppCompat.Light.NoActionBar in styles.xml that's why getSupportActionBar() is returning null , Check styles.xml file & change NoActionBar to DarkActionBar.

Full styles.xml file is here

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>


Upvotes: 2

Related Questions