не удается разрешить java.lang.ClassCastException: android.support.constraint.ConstraintLayout не может быть приведен к android.support.v7.widget.RecyclerView - PullRequest
0 голосов
/ 18 апреля 2019

Я боролся в течение нескольких часов, но не смог найти ошибку.Пожалуйста, помогите мне решить проблему.

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 голосов
/ 19 апреля 2019

Хотя я точно не могу решить проблему, я реализовал обходной путь.Вместо сочетания линейного макета и представления рециркулятора я просто использовал один вид рециркуляции в файле 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" />
0 голосов
/ 19 апреля 2019

Вам необходимо заменить LinearLayout на CardView в custom_list.xml.

<?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>
...