gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:45:10 25 4
gpt4 key购买 nike

当我为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com