Android:片段内的标签findViewById返回null?

时间:2015-03-16 17:23:20

标签: java android android-fragments tabs

我有一个包含片段的活动。在那个片段里面有一个按钮,当点击它时,它应该被一个带有标签(片段也在里面)的片段替换。

但是,当加载tabs片段并尝试按id查找其viewpager时,findViewById()将返回null。

这是我的代码: 带有按钮的片段内部:

@Override
public void onClick(View view) {

    switch (view.getId()) {
        case R.id.gewichtAktualiserenButton:
            //TODO implement tabs
            // replace current fragment with GewichtTabsFragment
            MainActivity mainActivity = (MainActivity)getActivity();
            GewichtTabsFragment gewichtTabsFragment = new GewichtTabsFragment();
            mainActivity.replaceFragment(gewichtTabsFragment, Constants.GEWICHT_TABS_FRAGMENT);
            break;
        case R.id.geburtsdatumEditText:
            showDatePickerDialog(view);
           break;
        case R.id.hundImageView:
            // display alert to choose from camera or gallery
            ImageSourceChooserDialog newFragment = new ImageSourceChooserDialog();
            newFragment.setHundFragment(this);
            newFragment.setWithDeleteItem(true);
            imgChooserDialog = newFragment;
            newFragment.show(getActivity().getSupportFragmentManager(), "image_source_chooser");
            break;
        case R.id.zusaetzeSwitch:
            // toggle enabled state of zusaetze EditText
            getZusaetzeEditText().setEnabled(!getZusaetzeEditText().isEnabled());
        default:
            break;
    }

GewichtTabsFragment.java:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_gewicht_tabs, container, false);

    MainActivity mainActivity = (MainActivity)getActivity();
    // Get the ViewPager and set it's PagerAdapter so that it can display items
    // the following call returns null? Why?
    ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
    viewPager.setAdapter(new GewichtFragmentPagerAdapter(mainActivity.getSupportFragmentManager()));

    // Give the PagerSlidingTabStrip the ViewPager
    PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) getActivity().findViewById(R.id.tabs);
    // Attach the view pager to the tab strip
    tabsStrip.setViewPager(viewPager);

    return view;
}

布局文件fragment_gewicht_tabs.xml:

<?xml version="1.0" encoding="utf-8"?>

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    app:pstsShouldExpand="true"
    app:pstsTextAllCaps="true"
    android:layout_width="match_parent"
    android:layout_height="48dp">
</com.astuetz.PagerSlidingTabStrip>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white" />

</LinearLayout>

为什么findViewById返回null?

3 个答案:

答案 0 :(得分:0)

它将为null,因为您正在尝试获取尚未创建的视图。所以更改

ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);

ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);

答案 1 :(得分:0)

改变这个:

PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip)   getActivity().findViewById(R.id.tabs);

到:

 PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);

答案 2 :(得分:0)

重写OnCreateView中的编码不正确。根据Google文档,该方法应仅返回视图,仅返回该视图。我在另一个onViewCreated的覆盖方法上做了这个,它发生在OnCreateView之后。请阅读@ onCreateView method

代码中onViewCreated()和onCreateView()的示例代码:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

// Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_gewicht_tabs, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
...
// Give the PagerSlidingTabStrip the ViewPager
    PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) view.findViewById...

然而其他发布的答案也是正确的。但我建议你在onViewCreated中完成这项工作。