Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MenuItemCompat.getActionView Returns Null

I am trying to implement a custom layout for my menu item and went through many solutions by I am getting Null pointer Exception whenever i try to fetch TextView inside the layout specified for actionLayout for my menuItem as the getActionView returns null. home_menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_notifications"
        android:title=""
        app:showAsAction="always"
        android:visible="true"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:orderInCategory="3"
        android:actionLayout="@layout/counter_action_bar_notification_icon"
        />
    <!--android:icon="@drawable/ic_notifications_black_24dp"-->
    <!--android:actionLayout="@layout/bage_layout"-->
</menu>

counter_action_bar_notification_icon

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_gravity="center"
    android:clickable="true"
    style="@android:style/Widget.ActionButton">

    <ImageView
        android:id="@+id/hotlist_bell"
        android:src="@drawable/ic_notifications_black_24dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_margin="0dp"
        android:contentDescription="bell"
    />

    <TextView
        android:id="@+id/hotlist_hot"
        android:layout_width="wrap_content"
        android:minWidth="17sp"
        android:textSize="12sp"
        android:textColor="#ffffffff"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@null"
        android:layout_alignTop="@id/hotlist_bell"
        android:layout_alignRight="@id/hotlist_bell"
        android:layout_marginRight="0dp"
        android:layout_marginTop="3dp"
        android:paddingBottom="1dp"
        android:paddingRight="4dp"
        android:paddingLeft="4dp"
        android:background="@drawable/bage_circle"/>
</RelativeLayout>

MainActivity:

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.home_menu, menu);
        item = menu.findItem(R.id.action_notifications);
        if(loginStatus==false){
            item.setVisible(false);
            return false;
        }
        if(user!=null && !user.getUserType().equals("3")){
            item.setVisible(false);
            return false;
        }

        final View menu_hotlist = MenuItemCompat.getActionView(item);
        TextView ui_hot =(TextView) menu_hotlist.findViewById(R.id.hotlist_hot);
        ui_hot.setText(Integer.toString(13));

        return true;

}
like image 661
Mehvish Ali Avatar asked Dec 23 '22 15:12

Mehvish Ali


2 Answers

Try this

item.setActionView(R.layout.counter_action_bar_notification_icon);

        final View menu_hotlist = MenuItemCompat.getActionView(item);
        TextView ui_hot =(TextView) menu_hotlist.findViewById(R.id.hotlist_hot);
        ui_hot.setText(Integer.toString(13));
like image 115
Faisal Naseer Avatar answered Jan 12 '23 00:01

Faisal Naseer


Use

app:actionLayout

instead of

android:actionLayout 

in your layout file and dont forget to define app

xmlns:app="http://schemas.android.com/apk/res-auto"
like image 24
Alper Avatar answered Jan 12 '23 02:01

Alper