简体   繁体   English

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

[英]Android: Tabs inside fragment findViewById returns null?

I have an activity which contains a fragment. 我有一个包含片段的活动。 Inside that fragment there is button, which should, when clicked, cause the fragment to be replaced by a fragment with tabs (fragments, too) inside. 在该片段内部有一个按钮,单击该按钮时应使片段被内部带有制表符(片段)的片段替换。

However, when the tabs fragment is loaded and i try to find its viewpager by id, findViewById() returns null. 但是,当标签片段被加载并且我尝试通过id查找其viewpager时,findViewById()返回null。

Here is my code: Inside the fragment with the button: 这是我的代码:在带有按钮的片段内:

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

The GewichtTabsFragment.java: 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;
}

And the layout file fragment_gewicht_tabs.xml: 布局文件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>

Why is findViewById return null? 为什么findViewById返回null?

It would be null because you are trying to get view which has not been created yet.So Change 它将为空,因为您正在尝试获取尚未创建的视图。

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

to

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

change this one : 改变这一点:

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

to : 至 :

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

Coding in override OnCreateView is not correct. 覆盖OnCreateView中的编码不正确。 According to the Google documentation, the method should return the view only, and only that. 根据Google文档,该方法应仅返回视图,并且仅返回该视图。 I did it on another override method onViewCreated, which occurs after OnCreateView. 我是在OnCreateView之后发生的另一个重写方法onViewCreated上完成的。 Please read @ onCreateView method . 请阅读@onCreateView方法

Sample code for onViewCreated() and onCreateView() from your code: 代码中的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...

However the other posted answers are correct also. 但是,其他发布的答案也是正确的。 But I recommend doing the work in onViewCreated, of course. 但是,我当然建议在onViewCreated中进行工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM