-1

Возможно, вы увидите пару одинаковых вопросов, но это не помогло мне решить эту проблему.java.lang.ClassCastException: android.support.v7.widget.RecyclerView не может быть отброшен в android.support.v4.widget.SwipeRefreshLayout

Мой XML содержит RecyclerView внутри SwipeRefreshLayout. Когда я запускаю свой код, он вызывается ниже исключения или сбоя приложения.

java.lang.RuntimeException: Невозможно начать деятельность ComponentInfo {}: java.lang.ClassCastException: android.support.v7.widget.RecyclerView не может быть приведен к android.support.v4.widget.SwipeRefreshLayout

Мой 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 код внутри активность:

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"); 
       } 
      }) 
    ); 
} 
+0

где вызывается initializeUI() ??? – sasikumar

+1

Изменить 'R.id.recyclerView_Incidents' на' R.id.swipeRefreshLayout_Incidents' для 'SwipeRefreshLayout' –

+0

@ ρяσѕρєяK Святое дерьмо !!! Какая глупость я сделал. В любом случае спасибо. – VVB

ответ

3

Вы используете неправильный идентификатор для mSwipeRefreshLayoutIncidents, вместо этого используйте R.id.swipeRefreshLayout_Incidents.

+0

Святое дерьмо !!! Какая глупость я сделал. В любом случае спасибо. – VVB

3

Эта ошибка означает, что вы отливаете RecyclerView в SwipeRefreshLayout:. Вместо этого:

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

использовать это:

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

Что я сделал меняется recyclerView_Incidents с swipeRefreshLayout_Incidents

Успехов

+0

Святое дерьмо !!! Какая глупость я сделал. В любом случае спасибо. – VVB

0

Под recyclerView_Incidents у вас есть RecyclerView (который не делает наследуется от SwipeRefreshLayout, как указано в documentation, следовательно исключение). Используйте id swipeRefreshLayout_Incidents в самой первой строке вашего метода вместо этого - я думаю, это было вашим намерением в первую очередь.