简体   繁体   中英

Setting adapter to a RecyclerView inside fragment causes Exception

I have a fragment, inside which there is a TabbedLayout , which contains three tabs(fragments) . Now The problem is when I open tab3 from tab1 , it causes a NullPointerException .

Here is the code to the 3rd Tab:

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


}

Now, I believe this is possibly caused by this method :

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

        adapter.notifyDataSetChanged();
    }
}

which tries to update the RecyclerView as soon as the tab is visible. Now, in order to fix this, I tried changing

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();
    }
}

But still, the problem is not solved. What's the ideal way to fix this?

> 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)

So, after spending some time around! I fixed it myself. The problem is that notifyDataSetChanged() is called when the View itself is null. So Here is how I fixed it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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