微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android.content.res.Resources对空对象引用的android.content.Context.getResources()’

我正在尝试在我的SlidingTabLayout上显示图标,所以在我的适配器中我已经创建了这样的东西,当我在寻找教程的时候我发现它我编辑了你将使用getDrawable的部分,因为它说它已经被弃用了应用我找到的解决方

@Override
public CharSequence getPageTitle(int position) {

    Drawable image = ResourcesCompat.getDrawable(mContext.getResources(),icons[position],null);
    image.setBounds(0,48,48);
    SpannableString sb = new SpannableString(" ");
    ImageSpan imageSpan = new ImageSpan(image,ImageSpan.ALIGN_BottOM);
    sb.setSpan(imageSpan,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.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(),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);
}

解决方法

看这里:

adapter = new ViewPagerAdapter(getSupportFragmentManager(),mContext);

从这里开始,mContext将变为null.因为你没有在onCreate方法中为mContext变量赋予任何参考.

将其更改为:

adapter = new ViewPagerAdapter(getSupportFragmentManager(),MainActivity.this);

或者您也可以试试这个:

在onCreate方法中:

mContext = MainActivity.this;

adapter = new ViewPagerAdapter(getSupportFragmentManager(),mContext);

使用此ViewPagerAdapter代码

public class ViewPagerAdapter extends FragmentStatePagerAdapter { 

CharSequence Titles[]; 
int NumbOfTabs;

private Activity mContext;

private int[] icons = {
        R.drawable.tabone,R.drawable.tabfive
}; 


public ViewPagerAdapter(FragmentManager fm,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,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return sb;
}





// This method return the Number of tabs for the tabs Strip 

@Override 
public int getCount() { 
    return NumbOfTabs;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐