Search code examples
androidnullpointerexceptiontextviewmenuitemfindviewbyid

TextView in menu is always null after orientation change


I have a TextView functioning as a notification badge on a menu item. The problem is that the findViewById method always returns null after an orientation change, so when I try to modify its visibility with setVisibility it throws a NullPointerException. I've tried calling the onCreateOptionsMenu again by calling invalidateOptionsMenu in onRestart but it doesn't appear to help.

From what I can tell every other view is found and it's just this TextView that is being a nuisance.

The part where it crashes (this is called in onCreate) :

public void updateUnreadNotificationCount(final int unreadNotificationsCount) {
    unreadNotificationCount = unreadNotificationsCount;
    if (unreadNotificationCount == 0) {
        notificationCounter.setVisibility(View.INVISIBLE);
    }else {
        notificationCounter.setVisibility(View.VISIBLE);
        notificationCounter.setText(String.valueOf(unreadNotificationCount));
    }
}

How the Textview gets created:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    View count = menu.findItem(R.id.action_notifications).getActionView();
    notificationCounter = (TextView) count.findViewById(R.id.textview_notification_count);
    count.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(mCtx, NotificationListActivity.class);
            startActivity(i);
        }
    });
    notificationCounter.setText(String.valueOf(unreadNotificationCount));
    return super.onCreateOptionsMenu(menu);
}

The XML for the menu item:

<item
    android:id="@+id/action_notifications"
    android:title="@string/action_notifications"
    android:icon="@drawable/ic_mail_white_48dp"
    android:actionLayout="@layout/actionbar_notification_icon"
    android:showAsAction="always"
    app:showAsAction="always"
    android:orderInCategory="60"/>

The action layout for the TextView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"
            style="@android:style/Widget.ActionButton">

<ImageView
    android:id="@+id/imageview_notification"
    android:src="@drawable/ic_mail_white_48dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_margin="0dp"/>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/textview_notification_count"
          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/imageview_notification"
          android:layout_alignRight="@id/imageview_notification"
          android:layout_marginRight="0dp"
          android:layout_marginTop="3dp"
          android:paddingBottom="1dp"
          android:paddingRight="4dp"
          android:paddingLeft="4dp"
          android:background="@drawable/rounded_square"/>

</RelativeLayout>

Solution

  • Managed to fix the issue by moving the initialization of the TextView into onPrepareOptionsMenu and calling invalidateOptionsMenu() during onResume().