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

问题描述 投票:0回答:2

我一直在苦苦挣扎几个小时但找不到错误。请帮我解决问题。

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>

但是对我来说一切似乎都很好,这里错了。

android android-layout android-recyclerview custom-adapter recyclerview-layout
2个回答
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>

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" />
© www.soinside.com 2019 - 2024. All rights reserved.