ContextCompat.getcolor() going to null object reference

2021-6-21 anglehua

I'm getting error when I set color to SlidingTabLayout object. Here is my mainActivity, first I found that getResource.getColor is deprecated. So I used contextCompat.getColor. But now its going to null.

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private ViewPager mPager;
    private SlidingTabLayout mTabs;
    private MyPagerAdapter adapter;
     Context context;

    protected void onCreate(Bundle savedInstanceState) {

        toolbar = (Toolbar) findViewById(;
        mPager = (ViewPager) findViewById(;
        mTabs = (SlidingTabLayout) findViewById(;

        assert getSupportActionBar() != null;

        NavigationDrawerFragment navigationDrawerFragment = (NavigationDrawerFragment)
        navigationDrawerFragment.setUp(, (DrawerLayout) findViewById(, toolbar);

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

        int bgColor = ContextCompat.getColor(context,R.color.colorAccent);
        mTabs.setSelectedIndicatorColors(ContextCompat.getColor(context, R.color.colorAccent));

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == {

            return true;
        if (id == {
            startActivity(new Intent(this, SubActivity.class));

        return super.onOptionsItemSelected(item);

    public static class MyFragment extends Fragment {
        private TextView textView;

        public static MyFragment getInstance(int position) {
            MyFragment myFragment = new MyFragment();
            Bundle args = new Bundle();
            args.putInt("position", position);
            return myFragment;

        public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container, Bundle savedInstanceState) {
            View layout = inflater.inflate(R.layout.fragment_my, container, false);
            textView = (TextView) layout.findViewById(;
            Bundle bundle = getArguments();
            if (bundle != null) {
            return layout;

    class MyPagerAdapter extends FragmentStatePagerAdapter {
        Context mContext;
        int icons[] = {R.drawable.home,R.drawable.hot_article,R.drawable.dizzy_person};
        String[] tabText = getResources().getStringArray(R.array.tabs);
        public MyPagerAdapter(FragmentManager fm,Context context ) {
            this.mContext = context;


        public Fragment getItem(int position) {
            MyFragment myFragment = MyFragment.getInstance(position);
            return myFragment;

        public CharSequence getPageTitle(int position) {
            Drawable drawable = ResourcesCompat.getDrawable(getResources(),icons[position],null);
            drawable.setBounds(0, 0, 36, 36);
            ImageSpan imageSpan = new ImageSpan(drawable);
            SpannableString spannableString = new SpannableString(" ");
            spannableString.setSpan(imageSpan,0,spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return spannableString;

        public int getCount() {
            return 3;

Is there any new method in marshmallow to solve this problem?

it is because context hasn't been initialized yet. I would recommend you not to have flying references to Context there and there. Activity is a subclass of Context. You can use directly this or NameOfActivity.this to access the context.

int bgColor = ContextCompat.getColor(context, R.color.colorAccent);

should be

int bgColor = ContextCompat.getColor(this, R.color.colorAccent);

You can solve this issue by getting context from the view

testview.setBackgroundColor(ContextCompat.getColor(testview.getContext(), R.color.colorBlack));


Powered by emlog 京ICP备15036472号-3 sitemap