actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) produce NullPointerException

后端 未结 2 644
无人共我
无人共我 2021-01-21 20:08
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActi         


        
相关标签:
2条回答
  • 2021-01-21 20:42

    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;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_collection_demo);
    
            // ViewPager and its adapters use support library
            // fragments, so use getSupportFragmentManager.
            mAdapter = new YourAdapter(getSupportFragmentManager());
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mAdapter);
        }
    

    The Adapter:

    public class YourAdapter extends FragmentStatePagerAdapter {
        private String[] titles = { "Item 1", "Item 2", "Item 3" };
        public YourAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int i) {
            switch(i){
               case 0:{
                  return new FragementA();
               }case 1:{
                  return new FragmentB();
               }case 2:{
                  return new FragmentC();
               }
            }
        }
    
        @Override
        public int getCount() {
            return titles.length;
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
    

    The Fragment that you will return and implementation of the onCreateView Method:

    public static class FragmentA extends Fragment {
        public static final String ARG_OBJECT = "object";
    
        @Override
        public View onCreateView(LayoutInflater inflater,
                ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.my_layout_file, container, false);
            //Simple implementation how to target text view in your layout
            TextView tv = (TextView)rootView.findViewById(R.id.my_text_view);
            return rootView;
        }
    }
    
    0 讨论(0)
  • 2021-01-21 20:49

    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..

    0 讨论(0)
提交回复
热议问题