我收到錯誤,當我設置顏色SlidingTabLayout對象。 這裏是我的mainActivity,首先我發現getResource.getColor已被棄用..所以我使用contextCompat.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;
}
}
}
是在有任何棉花糖新的方法來解決這個概率...幫助我..
感謝諮詢..從來沒有使用過飛參考, n .. –
不客氣 – Blackbelt