将适配器设置为RecyclerView内部片段会导致异常

时间:2016-05-21 07:05:30

标签: java android android-fragments

我有一个片段,里面有一个TabbedLayout,其中包含三个标签(片段)。现在问题是当我从tab3打开tab1时,它会导致NullPointerException

以下是第3个标签的代码:

    public class FavoritesFragment extends Fragment
{
    private RecyclerView rView;
    FavoritesAdapter adapter;

    @Override
    public void setMenuVisibility(final boolean visible) {
        super.setMenuVisibility(visible);
        if (visible)
        {

            adapter.notifyDataSetChanged();
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.favorite_fragment, container, false);
        Realm realm = Realm.getInstance(getActivity());
        RealmResults<favorite> results1 =
                realm.where(favorite.class).findAll();

        List<favorite> data  = results1;
        rView =(RecyclerView)rootView.findViewById(R.id.favorite_layout);
        GridLayoutManager glm_fav = new GridLayoutManager(getActivity(),3, LinearLayoutManager.VERTICAL,false);
        rView.setLayoutManager(glm_fav);
        adapter = new FavoritesAdapter(data);

        if(data!=null)
            adapter.notifyDataSetChanged();

        rView.setAdapter(adapter);
        return rootView;
    }


}

现在,我相信这可能是由这种方法引起的:

public void setMenuVisibility(final boolean visible) {
    super.setMenuVisibility(visible);
    if (visible)
    {

        adapter.notifyDataSetChanged();
    }
}

一旦选项卡可见,它会尝试更新RecyclerView。现在,为了解决这个问题,我尝试更改

public void setMenuVisibility(final boolean visible) {
    super.setMenuVisibility(visible);
    if (visible)
    {
        Realm realm = Realm.getInstance(getActivity());
    RealmResults<favorite> results1 =
            realm.where(favorite.class).findAll();

    List<favorite> data  = results1;
    rView =(RecyclerView)rootView.findViewById(R.id.favorite_layout);
    GridLayoutManager glm_fav = new GridLayoutManager(getActivity(),3, LinearLayoutManager.VERTICAL,false);
    rView.setLayoutManager(glm_fav);
    adapter = new FavoritesAdapter(data);

        adapter.notifyDataSetChanged();
    }
}

但是,问题仍未解决。解决这个问题的理想方法是什么?

> Here is the logcat also: D/AndroidRuntime: Shutting down VM
> E/AndroidRuntime: FATAL EXCEPTION: main
>                   Process: nanodegree.premiere, PID: 21859
>                   java.lang.NullPointerException: Attempt to invoke virtual method 'void
> nanodegree.premiere.adapter.FavoritesAdapter.notifyDataSetChanged()'
> on a null object reference
>                       at nanodegree.premiere.adapter.FavoritesFragment.setMenuVisibility(FavoritesFragment.java:29)
>                       at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:129)
>                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1104)
>                       at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
>                       at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
>                       at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
>                       at nanodegree.premiere.adapter.ListFragment$1.onTabSelected(ListFragment.java:52)
>                       at android.support.design.widget.TabLayout.selectTab(TabLayout.java:871)
>                       at android.support.design.widget.TabLayout.selectTab(TabLayout.java:841)
>                       at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1115)
>                       at android.support.design.widget.TabLayout$1.onClick(TabLayout.java:665)
>                       at android.view.View.performClick(View.java:5198)
>                       at android.view.View$PerformClick.run(View.java:21147)
>                       at android.os.Handler.handleCallback(Handler.java:739)
>                       at android.os.Handler.dispatchMessage(Handler.java:95)
>                       at android.os.Looper.loop(Looper.java:148)
>                       at android.app.ActivityThread.main(ActivityThread.java:5417)
>                       at java.lang.reflect.Method.invoke(Native Method)
>                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
>                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

所以,花了一些时间后!我自己修好了。问题是当View本身为null时调用notifyDataSetChanged()。所以这就是我修复它的方法:

@Override
public void setMenuVisibility(final boolean visible) {
    super.setMenuVisibility(visible);
    if (visible)
    {
        if(getView()!=null)
            adapter.notifyDataSetChanged();
    }
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.favorite_fragment, container, false);
    Realm realm = Realm.getInstance(getActivity());
    RealmResults<favorite> results1 =
            realm.where(favorite.class).findAll();

    List<favorite> data  = results1;
    rView =(RecyclerView)rootView.findViewById(R.id.favorite_layout);
    GridLayoutManager glm_fav = new GridLayoutManager(getActivity(),3, LinearLayoutManager.VERTICAL,false);
    rView.setLayoutManager(glm_fav);
    adapter = new FavoritesAdapter(data);

    if(data!=null)
        adapter.notifyDataSetChanged();

    rView.setAdapter(adapter);
    return rootView;
}