访问ViewPager片段视图

时间:2014-05-31 00:53:58

标签: android android-fragments nullpointerexception android-viewpager

我有一个viewpager,里面有2个片段页面。现在我想从第一个片段页面访问视图(textview,buttons),但每次我得到Nullpointer异常 看看我的代码

片段我想访问其观点:

    public class ListPagerFragment extends Fragment
    {
    ListView listview;
    TextView text;
    private ViewPager pager;

    public static ListPagerFragment newInstance(int message)
    {
        ListPagerFragment f = new ListPagerFragment();
        // pass the text message received from main activity into a bundle to be
        // delivered to the new fragment
        Bundle bdl = new Bundle();
        bdl.putInt("num", message);
        f.setArguments(bdl);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.list_pager_fragment, container, false);
        return v;
    }
    public void fillList(Nameslist list)
    {       
        listview = (ListView) getView().findViewById(R.id.listView1);
        if (listview == null)
            System.out.println("listview null"); //this should not be null but
//it is thus the nullpointerexception
        if (listview.getAdapter() == null)
        {
            MyListAdapter listAdapter = new MyListAdapter(getActivity(), R.layout.listview_item, list.getVenues());
            listview.setAdapter(listAdapter);
        }
        else
        {
            ((MyListAdapter) listview.getAdapter()).updateAdapter(list.getnames());
        } 

    }

PagerAdapter代码

public class MyPagerAdapter extends FragmentStatePagerAdapter
{
    private int mCount = 1;

    public MyPagerAdapter(FragmentManager fm)
    {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int position)
    {
        switch (position)
        {
        case 1:
            return ResultsPagerFragment.newInstance(position);
        }
        return ListPagerFragment.newInstance(position);
    }

    @Override
    public int getCount()
    {
        // TODO Auto-generated method stub
        return mCount;
    }
}

和主要的最终活动代码

private ViewPager pager;
    private MyPagerAdapter pagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(pagerAdapter);
}

以及稍后在活动的某个地方我有代码应该从Fragment触发fillList来完成这项工作

Fragment frag = pagerAdapter.getItem(0); //this should give me the Fragment i want right?
        ((ListPagerFragment)frag).fillList(result);

我在这里缺少什么?

2 个答案:

答案 0 :(得分:0)

这可能是因为您在ListPagerFragment.onCreateView()夸大了xml布局之前尝试使用Activity中的视图。将onCreate()内部的内容和fillList()的一些代码移动到onCreateView(),如下所示:

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

    listview = (ListView) v.findViewById(R.id.listView1);
    pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
    pager = (ViewPager) v.findViewById(R.id.pager);
    pager.setAdapter(pagerAdapter);

    return v;
}

在你的片段onCreateView()为视图充气后,你可以尝试填充它。

不要忘记清除视图引用并在onDestroyView()中取消注册listview侦听器(如果设置了一个)以避免内存泄漏,例如。

@Override
public void onDestroyView() {
    super.onDestroyView();
    listview.setOnItemClickListener(null);
    ...
    listview = null;
}

了解片段的生命周期 - > Fragment's Lifefycle

答案 1 :(得分:0)

好的,我发现问题出现在我的自定义FragmentStatePagerAdapter中 我所要做的就是在创建活动时创建片段并将它们放入ArrayList中,稍后将其传递给MyPagerAdapter

protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        frags = new ArrayList<Fragment>();
        frags.add(ListPagerFragment.newInstance(0));
        frags.add(ResultsPagerFragment.newInstance(1));

        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),frags);
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(pagerAdapter);

    }

然后像这样修改MyPagerAdapter:

public class MyPagerAdapter extends FragmentStatePagerAdapter
{
    private List<Fragment> fragments;

    public MyPagerAdapter(FragmentManager fm, List<Fragment> frags)
    {
        super(fm);
        this.fragments = frags;
    }

    @Override
    public Fragment getItem(int position)
    {   
        return fragments.get(position);
    }
}
有点傻吗?我不明白这一点,但它有效