java.lang.ClassCastException:android.support.v7.widget.RecyclerView无法强制转换为android.support.v4.widget.SwipeRefreshLayout

时间:2016-12-27 08:26:11

标签: android android-layout android-recyclerview classcastexception 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");
                }
            })
    );
}

3 个答案:

答案 0 :(得分:3)

您使用mSwipeRefreshLayoutIncidents的错误观看ID,而是使用R.id.swipeRefreshLayout_Incidents

答案 1 :(得分:3)

该错误意味着您将RecyclerView投射到SwipeRefreshLayout:而不是:

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

使用它:

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

我所做的就是用recyclerView_Incidents

更改swipeRefreshLayout_Incidents 祝你好运

答案 2 :(得分:0)

recyclerView_Incidents下,您有RecyclerView(不会从SwipeRefreshLayout继承,如documentation中所述,因此例外)。请在您方法的第一行使用ID swipeRefreshLayout_Incidents - 我认为这首先是您的意图。