从Android ViewPager中的片段访问方法

时间:2016-02-22 12:31:47

标签: android

我对android中的ViewPager很新。我成功实施了viewPager,但我无法使某些UI方法正常运行。

当我尝试修改某些TextView时,我得到NullPointerException(我知道NullPointer是什么,但我不明白如何修复它)

我有mPager的以下听众:

mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                play(position);

                ScreenSlidePagerAdapter fa = (ScreenSlidePagerAdapter) mPager.getAdapter();
                Fragment fragment = fa.getItem(position);

                if (fragment instanceof MusicSliderFragment) {
                    ((MusicSliderFragment) fragment).setSongName(splitName(myDataList.get(position))[0]);
                    ((MusicSliderFragment) fragment).setAuthor(splitName(myDataList.get(position))[1]);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

我的Fragment就是这样:

public class MusicSliderFragment extends Fragment {

    private TextView songName, songAuthor;
    private DiscreteSeekBar seekBar;

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

        seekBar = (DiscreteSeekBar) rootView.findViewById(R.id.songProgress);
        songName = (TextView) rootView.findViewById(R.id.songName);
        songAuthor = (TextView) rootView.findViewById(R.id.songAuthor);
        songName.setSelected(true);
        songAuthor.setSelected(true);

        return rootView;
    }

    public void setSongName(String name) {
        this.songName.setText(name);
    }

    public void setAuthor(String author) {
        this.songAuthor.setText(author);
    }
}

日志:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

我的适配器类:

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return new MusicSliderFragment();
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }

我的片段:

public class MusicSliderFragment extends Fragment {

    private TextView songName, songAuthor;

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

        songName = (TextView) rootView.findViewById(R.id.songName);
        songAuthor = (TextView) rootView.findViewById(R.id.songAuthor);
        songName.setSelected(true);
        songAuthor.setSelected(true);

        return rootView;
    }

    public void setSongName(String name) {
        this.songName.setText(name);
    }

    public void setAuthor(String author) {
        this.songAuthor.setText(author);
    }
}

ViewPager和适配器由以下内容创建:

mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);

感谢任何帮助,谢谢!

3 个答案:

答案 0 :(得分:0)

您可以从活动中传递适配器类中的对象,也可以从该对象中调用以片段

编写的任何方法

答案 1 :(得分:0)

如果您拨打适配器的getItem()。它只返回新实例化的片段,而不是ViewPager中的片段。您可以使用ViewPager搜索标记,在FragmentManager中获取该片段。

而不是使用

Fragment fragment = fa.getItem(position);

尝试使用:

String tag = "android:switcher:" + mPager.getId()+ ":" + position;
Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);

答案 2 :(得分:0)

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
MusicSliderFragment fragment
        public ScreenSlidePagerAdapter(FragmentManager fm,MusicSliderFragment fragment) {
            super(fm);
this.fragment = fragment;
        }

        @Override
        public Fragment getItem(int position) {
            return fragment;
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }



mPager = (ViewPager) findViewById(R.id.pager);

MusicSliderFragment fragment = new MusicSliderFragment();

        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(),fragment);
        mPager.setAdapter(mPagerAdapter);

using **"fragment"** object you can call any method of fragment in activity as well as in Adapterclass