Skip to content Skip to sidebar Skip to footer

ActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) Produce NullPointerException

import android.app.ActionBar; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; impor

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;

    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;
    }
}

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"