【发布时间】:2015-07-03 04:14:09
【问题描述】:
我正在尝试在我的 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;
}
我还在我的适配器上声明了这一点,我已经检查了我是否真的把它放在我的可绘制对象上并且所有这些都在那里。
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);
}
【问题讨论】:
-
您的 mContect 为空,您可以发布整个代码吗?
-
我把代码放在上面
-
我应该也把我的 MainActivity @AnandSingh?
-
@KairiSan 是的,我想要活动代码。
-
我把我的活动代码放在@AnandSingh 上面