Android.content.res.Resources android.content.Context.getResources () 'for null object reference

I am trying to display icons on my SlidingTabLayout, so in my adapter I created something like this, when I found it sometime when I am looking for a tutorial, I edited the part where you will use getDrawable because it talks about it already are 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 actually put it on my drawing and it is all 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 did wrong, but I always have 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 for image processing. 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);
}

      

+3


source to share


1 answer


See here:

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

      

From here it mContext

goes null

. Since you haven't provided any confirmation of the variable mContext

inside the method onCreate

.

change it to:

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

      



or you can also try this:

inside the method onCreate

:

mContext = MainActivity.this;

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

      

Use this code 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;
}

      

+3


source







All Articles