Actionbar.setnavigationmode(actionbar.navigation_mode_tabs) Produce Nullpointerexception
Solution 1:
Take a look at this one over here. This guy implements it very nice. However i would not suggest working with actionBar tabs because if you want to upgrade your app to later versions this method is deprecated. So you dont use the actionBar and you can use the following if you would like
YourAdapter mAdapter;
ViewPager mViewPager;
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collection_demo);
// ViewPager and its adapters use support library// fragments, so use getSupportFragmentManager.
mAdapter = newYourAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAdapter);
}
The Adapter:
publicclassYourAdapterextendsFragmentStatePagerAdapter {
private String[] titles = { "Item 1", "Item 2", "Item 3" };
publicYourAdapter(FragmentManager fm) {
super(fm);
}
@Overridepublic Fragment getItem(int i) {
switch(i){
case0:{
returnnewFragementA();
}case1:{
returnnewFragmentB();
}case2:{
returnnewFragmentC();
}
}
}
@OverridepublicintgetCount() {
return titles.length;
}
@Overridepublic CharSequence getPageTitle(int position) {
return titles[position];
}
}
The Fragment that you will return and implementation of the onCreateView
Method:
publicstaticclassFragmentAextendsFragment {
publicstaticfinalStringARG_OBJECT="object";
@Overridepublic View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
ViewrootView= inflater.inflate(R.layout.my_layout_file, container, false);
//Simple implementation how to target text view in your layoutTextViewtv= (TextView)rootView.findViewById(R.id.my_text_view);
return rootView;
}
}
Solution 2:
The real problem is in res/values/style.xml
and res/values-v14/style.xml
in the AppBaseTheme
(in two styles).
If change Theme.AppCompat.Light
and Theme.AppCompat.Light.DarkActionBar
by Theme.Holo.Light
and Theme.Holo.Light.DarkActionBar
in manifest down android:minSdkVersion
to version 11, it SOLVES the problem...
But it generates another problem: you lose AppCompat.Light theme
...
But error NullExceptionPointer
in actionbar.setNavigationmode
is solved..
Post a Comment for "Actionbar.setnavigationmode(actionbar.navigation_mode_tabs) Produce Nullpointerexception"