android – Refreshing a Fragment at reload-ThrowExceptions

Exception or error:

Refresh Fragment at reload

fragment_home.java

final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.yenile);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {

    //I want to restart the fragment I am in.

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
            }
        },500);
    }
});

How to solve:

I see that you want to refresh the fragment you are in, this is in fact the basic use case of SwypeRefresh, as Giorgios said you should say why does not work

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    swipeRefreshLayout.setOnRefreshListener(MainActivity.this);//is a Fragment so maybe you want to get the activity beyond the fragment with getActivity()
    swipeRefreshLayout.post(new Runnable() {
                                @Override
                                public void run() {
//put a log here:) Log.d("WHATEVERTAG", " I am running, yeah"
                                    swipeRefreshLayout.setRefreshing(true);
                                    setupViewPager(viewPager);
                                }
                            }
    );
}



 private void setupViewPager(ViewPager viewPager) {


swipeRefreshLayout.setRefreshing(false);


 }

Leave a Reply

Your email address will not be published. Required fields are marked *