我将颜色设置为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; } }}
棉花糖有什么新的方法来解决这个问题…帮帮我..
解决方法:
这是因为上下文尚未初始化.我建议你不要在那里和那里有对上下文的飞行引用. Activity是Context的子类.您可以直接使用此或nameOfActivity.this来访问上下文.
int bgcolor = ContextCompat.getcolor(context,R.color.colorAccent);
应该
int bgcolor = ContextCompat.getcolor(this ,R.color.colorAccent);
总结 以上是内存溢出为你收集整理的android – ContextCompat.getcolor()转到null对象引用全部内容,希望文章能够帮你解决android – ContextCompat.getcolor()转到null对象引用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)