我试图在我的SlidingTabLayout上显示图标,所以在我的适配器中我已经创建了这样的东西,当我找到教程的时候我发现它我编辑了你将使用的部分getDrawable因为它已经弃用并且应用我找到的解决方案
@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;
}
我也在我的适配器上声明这个,我已经检查了我是否真的把它放在我的drawable上并且所有它都在那里。
private int[] icons = {
R.drawable.tabone,
R.drawable.tabtwo,
R.drawable.tabthree,
R.drawable.tapfour,
R.drawable.tabfive
};
我不知道自己做错了什么,但我总是遇到这个错误
Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
即使我声明一个变量来处理图像。任何人都可以帮助我,我的最低版本是14,我的目标sdk是22,谢谢你提前。
这是我的适配器代码
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;
}
}
这是我的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);
}
答案 0 :(得分:3)
见这里:
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs, mContext);
从mContext
开始null
。因为您没有在mContext
方法中为onCreate
变量分配任何引用。
将其更改为:
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs, MainActivity.this);
或者你也可以试试这个:
在onCreate
方法内:
mContext = MainActivity.this;
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs, mContext);
使用此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;
}