【问题标题】:cannot resolve java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.support.v7.widget.RecyclerView无法解析 java.lang.ClassCastException:android.support.constraint.ConstraintLayout 无法转换为 android.support.v7.widget.RecyclerView
【发布时间】:2019-04-18 20:08:40
【问题描述】:

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

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>

但在我看来一切都很好,这里有什么问题。

【问题讨论】:

  • 是否可以将代码缩减为minimal, complete, verifiable example
  • 您正在使用 ConstraintLayout id 初始化 recyclerview。
  • 请分享您的 recyclerview 布局 xml 以供清晰参考
  • @SultanMahmud 供我参考,你怎么能说使用 ConstraintLayout id 初始化 recyclerview
  • @Quelklef 按要求编辑。请检查

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


【解决方案1】:

您需要将 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>

【讨论】:

    【解决方案2】:

    虽然我完全无法解决问题,但我实施了一种解决方法。而不是线性布局和回收器视图组合,我只是在 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" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 2017-02-22
      • 1970-01-01
      相关资源
      最近更新 更多