android - ContextCompat.getcolor() 转到空对象引用

标签 android android-context

当我为 SlidingTabLayout 对象设置颜色时出现错误。 这是我的 mainActivity,首先我发现 getResource.getColor 已被弃用。所以我使用了 contextCompat.getColor。但现在它将变为空。

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

棉花糖有没有新的方法来解决这个问题?

最佳答案

因为context还没有初始化。我建议你不要到处乱引用 ContextActivity 是 Context 的子类。您可以直接使用 thisNameOfActivity.this 来访问上下文。

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

应该是

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

关于android - ContextCompat.getcolor() 转到空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35624743/

相关文章:

android - 在 Android Alert 中编辑文本

android - 检索对 Log 的调用?例如Log.w() Log.e() Log.i()

android - 在 Kotlin Multiplatform 上复制到剪贴板

android - bada中是否有类似于android中的 "Context"的类?

java - 在另一个类中声明 View 元素(上下文不清楚)

android - 在 Android api 级别 27 中将 getContext() 覆盖为非空是个坏主意?

php - Android 应用程序向 mysql 数据库添加数据会抛出 JSONException

android - Firebase Firestore toObject 在 bool 属性映射上失败

android - 在 LinearLayout 中自动调整 ImageButtons 的大小

android - 将应用程序上下文获取到 Android 中的静态方法的最佳方法