Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContextCompat.getcolor() going to null object reference

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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.app_bar);
        mPager = (ViewPager) findViewById(R.id.pager);
        mTabs = (SlidingTabLayout) findViewById(R.id.tabs);

        setSupportActionBar(toolbar);
        assert getSupportActionBar() != null;
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        NavigationDrawerFragment navigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.fragment_nav_drawer);
        navigationDrawerFragment.setUp(R.id.fragment_nav_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

        adapter = new MyPagerAdapter(getSupportFragmentManager(),MainActivity.this);
        mPager.setAdapter(adapter);
        mTabs.setViewPager(mPager);
        mTabs.setDistributeEvenly(true);

        int bgColor = ContextCompat.getColor(context,R.color.colorAccent);
        mTabs.setBackgroundColor(bgColor);
        mTabs.setSelectedIndicatorColors(ContextCompat.getColor(context, R.color.colorAccent));
        mTabs.invalidate();
        mTabs.setCustomTabView(R.layout.custom_tab_view,R.id.tabText);
    }


    @Deprecated
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Deprecated
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {

            return true;
        }
        if (id == R.id.navigate) {
            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);
            myFragment.setArguments(args);
            return myFragment;
        }


        @Override
        public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container, Bundle savedInstanceState) {
            View layout = inflater.inflate(R.layout.fragment_my, container, false);
            textView = (TextView) layout.findViewById(R.id.position);
            Bundle bundle = getArguments();
            if (bundle != null) {
                textView.setText(bundle.getInt("position"));
            }
            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 ) {
            super(fm);
            this.mContext = context;

        }

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

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

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

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

like image 704
Sagar Chavada Avatar asked Feb 25 '16 10:02

Sagar Chavada


2 Answers

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);
like image 150
Blackbelt Avatar answered Nov 18 '22 07:11

Blackbelt


You can solve this issue by getting context from the view

testview.setBackgroundColor(ContextCompat.getColor(testview.getContext(), R.color.colorBlack));
like image 6
Dhaval Jivani Avatar answered Nov 18 '22 09:11

Dhaval Jivani