-1

にキャストすることはできません。とjava.lang.ClassCastException:android.support.v7.widget.RecyclerViewはたぶん、あなたは夫婦同じ質問が表示されますが、それらは私がこの問題を解決する助けにはならなかったandroid.support.v4.widget.SwipeRefreshLayout

私のXMLはRecyclerViewの中にSwipeRefreshLayoutを含んでいます。コードを実行すると、例外またはアプリクラッシュの下にスローされます。

java.lang.RuntimeException: にjava.lang.ClassCastException:ComponentInfo {}アクティビティを開始できません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'を' SwipeRefreshLayout'の 'R.id.swipeRefreshLayout_Incidents'に変更しました –

+0

@ρяσѕρєяKうわー!!!私が作ったどんな些細な間違い。とにかくおかげさま。 – VVB

答えて

3

mSwipeRefreshLayoutIncidentsに間違った表示IDを使用していますが、代わりにR.id.swipeRefreshLayout_Incidentsを使用してください。

+0

聖なるくそ!!!私が作ったどんな些細な間違い。とにかくおかげさま。 – VVB

3

このエラーは、RecyclerViewSwipeRefreshLayoutにキャストしていることを意味します。この代わりに:

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

使用この:私は

+0

ほんとうに!私が作ったどんな些細な間違い。とにかくおかげさま。 – VVB

0

recyclerView_Incidentsの下swipeRefreshLayout_Incidents

幸運でrecyclerView_Incidentsを変更している行っています何

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

あなたが持ってRecyclerView(ありませんdocumentationで述べたようにSwipeRefreshLayoutから継承しているため、例外です)。その代わりにメソッドの最初の行にid swipeRefreshLayout_Incidentsを使用してください - 私はそれが最初のあなたの意図だったと思います。

関連する問題