Ben Luk
Ben Luk

Reputation: 755

getActivity().getActionBar() on first fragment crash

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupViewPager();

    }

    private void setupViewPager() {
        // TODO Auto-generated method stub
        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(), FragmentPagerItems.with(this)
                        .add("Home", HomeFragment.class)
                        .add("Message", MessageFragment.class)
                        .add("My", MyFragment.class).create());

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(adapter);
        SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
        final LayoutInflater inflater = LayoutInflater.from(viewPagerTab.getContext());
        final Resources res = viewPagerTab.getContext().getResources();
        viewPagerTab.setCustomTabView(new SmartTabLayout.TabProvider() {
            @Override
            public View createTabView(ViewGroup container, int position,PagerAdapter adapter) {
                ImageView icon = (ImageView) inflater.inflate(R.layout.mainactivity_tab, container, false);
                switch (position) {
                case 0:
                    icon.setImageDrawable(res.getDrawable(R.drawable.mainactivity_tab_icon_home));
                    break;
                case 1:
                    icon.setImageDrawable(res.getDrawable(R.drawable.mainactivity_tab_icon_message));
                    break;
                case 2:
                    icon.setImageDrawable(res.getDrawable(R.drawable.mainactivity_tab_icon_my));
                    break;
                default:
                    throw new IllegalStateException("Invalid position: "+ position);
                }
                return icon;
            }
        });
        viewPagerTab.setViewPager(viewPager);

    }

Here is the code in that three fragment

public class HomeFragment extends Fragment {
    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_home, container, false);

        TextView title = (TextView) rootView.findViewById(R.id.item_title);
        title.setText("Home");

        return rootView;
    }

    @Override
    public void setMenuVisibility(final boolean visible) {
        super.setMenuVisibility(visible);
        if (visible) {
            Log.d("Action bar", "Home");

            ActionBar mActionBar= getActivity().getActionBar();
            mActionBar.setDisplayShowHomeEnabled(true);
            mActionBar.setDisplayShowTitleEnabled(false);

            LayoutInflater mInflater = LayoutInflater.from(getActivity());
            View mCustomView = mInflater.inflate(R.layout.actionbar_message, null);
            mActionBar.setCustomView(mCustomView);
            mActionBar.setDisplayShowCustomEnabled(true);
            TextView title = (TextView) mCustomView.findViewById(R.id.actionbar_message_title);

            title.setText("Home action bar");
        }
    }

They work on MessageFragment.class and MyFragment but not HomeFragment. It will crash and ActionBar mActionBar = getActivity().getActionBar(); will return null and crash.

Why it only works on MessageFragment and MyFragment?

HomeFragment is the first fragment to show.

My idea: I will to use different custom action bar on different fragment.

Upvotes: 1

Views: 4712

Answers (3)

Ben Luk
Ben Luk

Reputation: 755

I god the solutions, put my code in setUserVisibleHint

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {

    }
}

Upvotes: 0

Simas
Simas

Reputation: 44158

The problem is that your fragment is not attached to an activity when setMenuVisibility is called. Therefore getActivity returns null.

I don't understand why you'd want to mess with this method in the first place. You can just modify the the action bar in your onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_home, container, false);

    {
        TextView title = (TextView) rootView.findViewById(R.id.item_title);
        title.setText("Home");
    }

    ActionBar mActionBar= getActivity().getActionBar();
    mActionBar.setDisplayShowHomeEnabled(true);
    mActionBar.setDisplayShowTitleEnabled(false);

    LayoutInflater mInflater = LayoutInflater.from(getActivity());
    View mCustomView = mInflater.inflate(R.layout.actionbar_message, null);
    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true);
    TextView title = (TextView) mCustomView.findViewById(R.id.actionbar_message_title);

    title.setText("Message action bar");

    return rootView;
}

If really do need the use of that method, then you could try a simple null check:

if (visible && getActivity() != null) {

Upvotes: 4

Praveena
Praveena

Reputation: 6941

Replace this line

ActionBar mActionBar = getActivity().getActionBar();

by this

ActionBar mActionBar=((ActionBarActivity) getActivity()).getSupportActionBar();

It's because you extended ActionBarActivity

Upvotes: 5

Related Questions