Skip to content Skip to sidebar Skip to footer

Getting A Null Pointer Exception When Setting Up The Tab Icons

I am trying to setup icons in the tabs, but I am keep getting null pointer exception.I am unable to understand as to why is happening? protected void onCreate(Bundle savedInstanceS

Solution 1:

You are facing this issue because you have not added tabs and try to set icon

//Add tabs icon with setIcon() or simple text with .setText()
 tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.ic_home));
 tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.ic_profile));
 tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.ic_settings));

For further details check the link below

https://gist.github.com/faizsiddiqui/9af9cea9051335e1ad3f


Solution 2:

To get the view either use:

layout.getChildAt(0).getChildAt(0).setIcon(R.drawable.ic_camera);
layout.getChildAt(0).getChildAt(1).setIcon(R.drawable.ic_instagram);

or

layout.getChildAt(0).findViewById(R.id.your_camera_tab_id).setIcon(R.drawable.ic_camera);
layout.getChildAt(0).findViewById(R.id.your_instagram_tab_id).setIcon(R.drawable.ic_instagram);

layout.getChildAt(0) returns the ViewGroup holding the Tabs.


Post a Comment for "Getting A Null Pointer Exception When Setting Up The Tab Icons"