无法解析java.lang.ClassCastException:android.support.constraint.ConstraintLayout无法转换为android.support.v7.widget.RecyclerView

时间:2019-04-18 20:08:40

标签: android android-layout android-recyclerview custom-adapter recyclerview-layout

我已经努力了几个小时,但找不到错误。请帮助我解决问题。

custom_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="10dp"
    card_view:cardElevation="5dp"
    card_view:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:text="Name"
            android:layout_marginTop="5dp"
            android:textAppearance="?android:attr/textAppearanceLarge"/>

        <TextView
            android:id="@+id/org"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Organization"
            android:layout_marginBottom="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:textAppearance="?android:attr/textAppearanceMedium"/>

    </LinearLayout>

</android.support.v7.widget.CardView>

UserHome.java 该错误专门在initviews()的以下行中。

recyclerView = (RecyclerView)findViewById(R.id.recycler_view);

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userhome);
        initviews();
        fetchdata();



    }

private void initviews() {   
        recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

    }


    private void fetchdata() {

        db.collection("pos-operators")
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot documentSnapshots) {

                        List<User> favModel  = documentSnapshots.toObjects(User.class);
                        Log.w(TAG,"fetch successful : "+favModel.size());
                        for(int i=0;i<favModel.size();i++){
                            Log.w(TAG,"data"+favModel.get(i).getUsername()+" "+favModel.get(i).getOrg());
                        }
                        UsersAdapter customAdapter = new UsersAdapter(favModel);
                        recyclerView.setAdapter(customAdapter);
                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.w(TAG,"failed to fetch");
            }
        });

    }

    @Override
    protected void onStart(){
        super.onStart();

        db.collection("pos-operators").addSnapshotListener(this,new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
                if(documentSnapshots.isEmpty()){
                    Log.w("UserHome: ","Exec "+e);
                }else{
                    List<User> favModel  = documentSnapshots.toObjects(User.class);
                    UsersAdapter customAdapter = new UsersAdapter(favModel);
                    recyclerView.setAdapter(customAdapter);

                    Log.w("","Size onstart "+favModel.size());
                }
            }
        });
    }


}

Content_userhome.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 

xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".UserHome"
    tools:showIn="@layout/app_bar_userhome">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view"/>
</android.support.constraint.ConstraintLayout>

但是对我来说,一切似乎都很好,这是怎么回事。

2 个答案:

答案 0 :(得分:0)

您需要将custom_list.xml中的 LinearLayout 替换为 CardView

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="10dp"
    card_view:cardElevation="5dp"
    card_view:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:text="Name"
            android:layout_marginTop="5dp"
            android:textAppearance="?android:attr/textAppearanceLarge"/>

        <TextView
            android:id="@+id/org"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Organization"
            android:layout_marginBottom="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:textAppearance="?android:attr/textAppearanceMedium"/>
    </LinearLayout>
</android.support.v7.widget.CardView>

答案 1 :(得分:0)

尽管我确实无法解决问题,但我还是解决了问题。除了线性布局和回收站视图组合之外,我仅在content_userhome.xml中仅使用回收站视图。

<?xml version="1.0" encoding="utf-8"?>


<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="100sp"
    android:id="@+id/recycler_view"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".UserHome"
    tools:showIn="@layout/app_bar_userhome"
    xmlns:android="http://schemas.android.com/apk/res/android" />