I am working on my java snippet that I want to check if the TextView in the menu is exist or not. If the textview is not exist then I want to return it as a null so I could do something.
When I try this:
if (total_inbox_unread > 0) {
@SuppressLint("ResourceType") TextView total_inbox_tv = (TextView)
menu.getItem(0).getActionView().findViewById(R.id.total_mailbox);
}
if (total_inbox_tv == null) {
...do something
}
I am getting this:
Process: com.loginpage, PID: 29159 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
Here is the full code:
NavigationView navigationView = (NavigationView)
_fragment.getActivity().findViewById(R.id.nav_view);
Menu menu = (Menu) navigationView.getMenu();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.menu_items, null);
if (total_inbox_unread > 0) {
@SuppressLint("ResourceType") TextView total_inbox_tv = (TextView) menu.getItem(0).getActionView().findViewById(R.id.total_mailbox);
//LinearLayout mailbox_layout = (LinearLayout) menu.getItem(0).getActionView().findViewById(R.id.mailbox_layout);
if (total_inbox_tv == null) {
//LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.menu_items, null);
//TextView total_mailbox_tv = (TextView) linearLayout.findViewById(R.id.total_mailbox);
total_inbox_tv = linearLayout.findViewById(R.id.total_mailbox);
int total_mailbox = total_inbox_unread;
total_inbox_tv.setText(Integer.toString(total_mailbox));
total_inbox_tv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
menu.getItem(0).setActionView(linearLayout);
}
else
{
int total_inbox = Integer.parseInt(total_inbox_tv.getText().toString());
if (total_inbox_unread > total_inbox) {
total_inbox_tv.setText(Integer.toString(total_inbox_unread));
}
else if (total_inbox_unread < total_inbox) {
total_inbox_tv.setText(Integer.toString(total_inbox_unread));
}
}
}
else if (total_inbox_unread == 0) {
if (!(menu.getItem(0).getActionView().findViewById(R.id.total_mailbox) == null)) {
LinearLayout inbox_layout = (LinearLayout) menu.getItem(0).getActionView().findViewById(R.id.mailbox_layout);
TextView total_inbox_tv = (TextView) menu.getItem(0).getActionView().findViewById(R.id.total_mailbox);
int total_inbox = Integer.parseInt(total_inbox_tv.getText().toString());
if (total_inbox >= 1) {
inbox_layout.removeAllViews();
inbox_layout.invalidate();
}
}
}
There is no textview in the subgroup under the menu. I can only add the submenu
How I can get the textview to return as a null when I have no textview in the menu??
Thank you.