android.content.Context.getResources()在空对象引用上

5

我正在尝试在我的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 是空的,你能贴出整个代码吗? - Anand Singh
我把代码放在上面。 - Kairi San
我应该也把我的MainActivity放在@AnandSingh吗? - Kairi San
@KairiSan 是的,我想要活动代码。 - Anand Singh
我将我的活动代码放在@AnandSingh之上。 - Kairi San
@KairiSan,请检查我的答案。 - Anand Singh
1个回答

5
看这里:
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs, mContext);

从这里开始,mContext 变成了 null。因为你在 onCreate 方法中没有给 mContext 变量分配任何引用。

修改为:

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;
}

我尝试了这段代码,但是它仍然没有显示图标 :( 但是我现在没有任何错误。 - Kairi San
@KairiSan,请检查我的编辑答案,主要是`getPageTitle'方法的代码。我希望这次您也能看到图片。 - Anand Singh
它仍然没有显示我的图标。还有其他原因吗?为什么它不显示我的图标? - Kairi San
@KairiSan 看一下这篇帖子 https://dev59.com/9WAg5IYBdhLWcg3wjbZt 希望能解决你的图标问题。无论如何,我认为你应该接受这个答案,因为我解决了你的“空对象引用”问题。 - Anand Singh
请不要将Context存储在成员变量中,因为这会导致您的Activity泄漏。 - Bradford2000

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接