第一个片段崩溃时的getActivity()。getActionBar()

时间:2015-06-14 05:37:57

标签: android android-actionbar fragment

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

    }

这是三个片段中的代码

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

他们在MessageFragment.class和MyFragment上工作,但不在HomeFragment上工作。 它会崩溃,ActionBar mActionBar = getActivity().getActionBar();将返回null并崩溃。

为什么它只适用于MessageFragment和MyFragment?

HomeFragment是第一个展示的片段。

我的想法:我将在不同的片段上使用不同的自定义操作栏。

3 个答案:

答案 0 :(得分:5)

替换此行

ActionBar mActionBar = getActivity().getActionBar();

由此

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

这是因为您延长了ActionBarActivity

答案 1 :(得分:4)

问题是,在调用setMenuVisibility时,您的片段未附加到活动。因此getActivity会返回null

我不明白为什么你想要首先搞乱这种方法。您只需修改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 (visible && getActivity() != null) {

答案 2 :(得分:0)

我说解决方案,把我的代码放在setUserVisibleHint

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

    }
}