【问题标题】:Android: Tabs inside fragment findViewById returns null?Android:片段findViewById内的选项卡返回null?
【发布时间】:2015-03-16 17:23:19
【问题描述】:

我有一个包含片段的活动。在该片段内部有一个按钮,当单击该按钮时,应该会导致片段被内部带有选项卡(也是片段)的片段替换。

但是,当标签片段被加载并且我尝试通过 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?

【问题讨论】:

    标签: java android android-fragments tabs


    【解决方案1】:

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

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

    到

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

    【讨论】:

      【解决方案2】:

      改变这个:

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

      到:

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

      【讨论】:

      • 因为你在视图中找到的 PagerSlidingTabStrip 已经膨胀了
      【解决方案3】:

      重写 OnCreateView 中的编码不正确。根据谷歌文档,该方法应该只返回视图,并且只返回那个。我是在另一个覆盖方法 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 中进行工作。

      【讨论】:

        猜你喜欢
        • 2014-09-15
        • 2021-11-23
        • 2014-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多