I'm trying to display icons on my SlidingTabLayout so in my adapter i've created something like this where i found it sometime when i'm looking for tutorial i edited the part where you will use the getDrawable because it says its already deprecated and apply the solution i found
@Override
public CharSequence getPageTitle(int position) {
Drawable image = ResourcesCompat.getDrawable(mContext.getResources(), icons[position], null);
image.setBounds(0, 0, 48, 48);
SpannableString sb = new SpannableString(" ");
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}
i also declare this on my adapter where i already check if i really put it on my drawable and all of it are there.
private int[] icons = {
R.drawable.tabone,
R.drawable.tabtwo,
R.drawable.tabthree,
R.drawable.tapfour,
R.drawable.tabfive
};
i don't know what i've done wrong but i'm always having this error
Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
even if i declare a variable to handle the images. Can anyone help me please my min version is 14 and my target sdk is 22. thank you in advance.
here is my code for my adapter
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[];
int NumbOfTabs;
Context mContext;
private int[] icons = {
R.drawable.tabone,
R.drawable.tabtwo,
R.drawable.tabthree,
R.drawable.tapfour,
R.drawable.tabfive
};
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb, Context context) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
this.mContext = context;
}
@Override
public Fragment getItem(int position) {
if(position == 0) // if the position is 0 we are returning the First tab
{
Tabone tab_one = new Tabone();
return tab_one;
}
else if(position == 1)
{
Tabtwo tab_two = new Tabtwo();
return tab_two;
}else if(position == 2){
Tabthree tab_three = new Tabthree();
return tab_three;
}else if(position == 3){
Tabfour tab_four = new Tabfour();
return tab_four;
}else{
Tabfive tab_five = new Tabfive();
return tab_five;
}
}
@Override
public CharSequence getPageTitle(int position) {
Drawable image = ResourcesCompat.getDrawable(mContext.getResources(), icons[position], null);
image.setBounds(0, 0, 48, 48);
SpannableString sb = new SpannableString(" ");
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}
// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
return NumbOfTabs;
}
}
here is my MainActivity
private ViewPager mPager;
private ViewPagerAdapter adapter;
private SlidingTabLayout mTabs;
private Context mContext;
CharSequence Titles[] = {"One", "Two", "Three", "Four", "Five"};
int Numboftabs = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs, mContext);
// Assigning ViewPager View and setting the adapter
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(adapter);
// Assigning the Sliding Tab Layout View
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setDistributeEvenly(true);
// Setting the ViewPager For the SlidingTabsLayout
mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.tabsIndicator));
mTabs.setViewPager(mPager);
}
See here:
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs, mContext);
From here mContext
is going null
. As you didn't assigned any refrence to mContext
variable inside onCreate
method.
change it to:
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs, MainActivity.this);
or you can also try this:
inside onCreate
method:
mContext = MainActivity.this;
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs, mContext);
Use this code of ViewPagerAdapter
:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[];
int NumbOfTabs;
private Activity mContext;
private int[] icons = {
R.drawable.tabone,
R.drawable.tabtwo,
R.drawable.tabthree,
R.drawable.tapfour,
R.drawable.tabfive
};
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb, Activity context) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
this.mContext = context;
}
@Override
public Fragment getItem(int position) {
if(position == 0) // if the position is 0 we are returning the First tab
{
Tabone tab_one = new Tabone();
return tab_one;
}
else if(position == 1)
{
Tabtwo tab_two = new Tabtwo();
return tab_two;
}else if(position == 2){
Tabthree tab_three = new Tabthree();
return tab_three;
}else if(position == 3){
Tabfour tab_four = new Tabfour();
return tab_map;
}else{
Tabfive tab_five = new Tabfive();
return tab_five;
}
}
@Override
public CharSequence getPageTitle(int position) {
Drawable image = mContext.getResources().getDrawable(icons[position]);
image.setBounds(0, 0, 48, 48);
SpannableString sb = new SpannableString(" ");
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}
// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
return NumbOfTabs;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With