ContextCompat.getcolor()转到null对象引用

时间:2016-02-25 10:31:31

标签: android android-context

当我将颜色设置为SlidingTabLayout对象时出现错误。 这是我的mainActivity,首先我发现不推荐使用getResource.getColor ..所以我使用了contextCompat.getColor ..但现在它将为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;
        }
    }
}

棉花糖中是否有任何新方法可以解决这个问题...帮助我..

6 个答案:

答案 0 :(得分:10)

这是因为上下文尚未初始化。我建议你不要在那里和那里有Context的飞行参考。 Activity是Context的子类。您可以直接使用thisNameOfActivity.this来访问上下文。

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

应该是

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

答案 1 :(得分:0)

context更改为MainActivity.this

使用这种方式

int bgColor = ContextCompat.getColor(MainActivity.this,R.color.colorAccent);
mTabs.setBackgroundColor(bgColor);
mTabs.setSelectedIndicatorColors(ContextCompat.getColor(MainActivity.this, R.color.colorAccent));

答案 2 :(得分:0)

使用localhost.localdomain并检查您的上下文是否为null或您的color_name值是否实际存在

答案 3 :(得分:0)

它是一项活动。 所以我们可以使用,

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

如果是Fragment 我们可以使用,

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

答案 4 :(得分:0)

如果您尝试基于异步数据库调用修改视图,您可能希望将上下文传递给执行数据库调用的方法 你甚至可以关注this

  private void dbCall(Activity activity){

      // perform the database call 
      int bgColor = ContextCompat.getColor(activity,R.color.colorAccent);

}

答案 5 :(得分:0)

您可以通过从视图中获取上下文

来解决此问题
testview.setBackgroundColor(ContextCompat.getColor(testview.getContext(), R.color.colorBlack));