-1

Maybe you will see couple same questions but those didn't help me to solve this.

My XML contains RecyclerView inside SwipeRefreshLayout. When I run my code, it throws below exception or app crashes.

java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.ClassCastException: android.support.v7.widget.RecyclerView cannot be cast to android.support.v4.widget.SwipeRefreshLayout

My XML :

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rlList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rlSearch"
        android:layout_above="@+id/rlBottomCreateIncident"
        android:layout_margin="5dp">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout_Incidents"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView_Incidents"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>

        </android.support.v4.widget.SwipeRefreshLayout>
    </RelativeLayout>

Java Code inside Activity :

private void initializeUI() {

    //Initialize swipe to refresh view
    mSwipeRefreshLayoutIncidents = (SwipeRefreshLayout) findViewById(R.id.recyclerView_Incidents);
    mSwipeRefreshLayoutIncidents.setColorScheme(android.R.color.holo_blue_bright,
            android.R.color.holo_green_light,
            android.R.color.holo_orange_light,
            android.R.color.holo_red_light);
    mSwipeRefreshLayoutIncidents.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            //Refreshing data on server
        }
    });

    mSwipeRefreshLayoutIncidents.setRefreshing(true);

    recyclerViewIncidents = (RecyclerView) findViewById(R.id.recyclerView_Incidents);
    sp_incidentAdapter = new SP_IncidentAdapter(context, sp_incidentListItemArrayList);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerViewIncidents.setLayoutManager(mLayoutManager);
    recyclerViewIncidents.setItemAnimator(new DefaultItemAnimator());
    recyclerViewIncidents.setAdapter(sp_incidentAdapter);
    recyclerViewIncidents.addOnItemTouchListener(new RecyclerItemClickListener(context,
            new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    // TODO Handle item click
                    Log.v("item click", " working fine");
                }
            })
    );
}
halfer
  • 19,824
  • 17
  • 99
  • 186
VVB
  • 7,363
  • 7
  • 49
  • 83
  • where you call initializeUI()??? – sasikumar Dec 27 '16 at 08:28
  • 1
    Change `R.id.recyclerView_Incidents` to `R.id.swipeRefreshLayout_Incidents` for `SwipeRefreshLayout` – ρяσѕρєя K Dec 27 '16 at 08:29
  • @ρяσѕρєяK Holy crap!!! What a silliy mistake I made. Anyways Thanks. – VVB Dec 27 '16 at 08:32
  • Side note: we like questions that are succinct and to the point here. There is no need to add [please-help begging](https://stackoverflow.com/search?q=user%3A4390557+please+help) to all of your questions. It does not make answers faster or more likely - just gives editors more work to do. – halfer Dec 27 '16 at 09:57

3 Answers3

3

You are using wrong view id for mSwipeRefreshLayoutIncidents, use R.id.swipeRefreshLayout_Incidents instead.

3

That error means that you are casting RecyclerView to SwipeRefreshLayout:. Instead of this:

mSwipeRefreshLayoutIncidents = (SwipeRefreshLayout) findViewById(R.id.recyclerView_Incidents);

use this:

mSwipeRefreshLayoutIncidents = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout_Incidents);

What i am done is changing recyclerView_Incidents with swipeRefreshLayout_Incidents

Good luck

Charuක
  • 12,953
  • 5
  • 50
  • 88
Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
0

Under recyclerView_Incidents you have a RecyclerView (which does not inherit from SwipeRefreshLayout, as said in documentation, hence the exception). Use id swipeRefreshLayout_Incidents in the very first line of your method instead - I think that was your intention in the first place.

Charuක
  • 12,953
  • 5
  • 50
  • 88
rasmeta
  • 465
  • 2
  • 12