尝试在My HomeFragment中实施RecyclerView时,应用已停止

时间:2017-08-04 20:19:21

标签: java android

我正在关注一个教程&我尝试通过添加适配器将RecyclerView添加到其中并在我的HomeFragment中显示,但是当我在模拟器上运行项目时,我的应用程序很快停止了。

我有我的HomeFragment,我的适配器,我的模型

My HomeFragment.java

package com.medum.medum.model;

import android.widget.TextView;

public class Picture {
private String picture;
private String titlecard;
private int pricecard = 0;
private String descriptioncard;
private String timecard = "0";

public Picture(String picture, String titlecard, int pricecard, String descriptioncard, String timecard) {
    this.picture = picture;
    this.titlecard = titlecard;
    this.pricecard = pricecard;
    this.descriptioncard = descriptioncard;
    this.timecard = timecard;
}

public String getPicture() {
    return picture;
}

public void setPicture(String picture) {
    this.picture = picture;
}

public String getTitlecard() {
    return titlecard;
}

public void setTitlecard(String titlecard) {
    this.titlecard = titlecard;
}

public int getPricecard() {
    return pricecard;
}

public void setPricecard(int pricecard) {
    this.pricecard = pricecard;
}

public String getDescriptioncard() {
    return descriptioncard;
}

public void setDescriptioncard(String descriptioncard) {
    this.descriptioncard = descriptioncard;
}

public String getTimecard() {
    return timecard;
}

public void setTimecard(String timecard) {
    this.timecard = timecard;
}
}

我的适配器 - CardAdapterRecyclerView.java

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:orientation="vertical"
    android:weightSum="4">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="3.2"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal">

            <ImageView
                android:id="@+id/coverImageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:scaleType="centerCrop"
                android:background="@drawable/prueba"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left|bottom"
                android:background="#8c0085B2"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/cardtitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="16dp"
                    android:textSize="25sp"
                    android:textColor="#FFFFFF"
                    android:textStyle="bold"
                    android:text="Casa en venta"/>

                <TextView
                    android:id="@+id/price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="16dp"
                    android:textSize="20sp"
                    android:textColor="#FFFFFF"
                    android:textStyle="bold"
                    android:text="$145,000.000 MXN"/>
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.8"
        android:gravity="center|right"
        android:orientation="horizontal">

            <TextView
                android:id="@+id/description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/description"
                android:layout_marginRight="8dp"
                />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hace"
            android:layout_marginRight="5dp"/>
        <TextView
            android:id="@+id/timecard"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4 dias"
            android:layout_marginRight="8dp"/>

        <CheckBox
            android:id="@+id/Favorite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/fav_selected"
            />

        <ImageView
            android:id="@+id/messageuser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:src="@drawable/chat" />
    </LinearLayout>

</LinearLayout>

我的模特 - Picture.java

08-04 12:58:39.870 9223-9223/com.medum.medum E/AndroidRuntime: FATAL EXCEPTION: main
                                                           Process: com.medum.medum, PID: 9223
                                                           java.lang.ClassCastException: android.support.v7.widget.CardView cannot be cast to android.widget.ImageView
                                                               at com.medum.medum.adapter.CardAdapterRecyclerView$CardViewHolder.<init>(CardAdapterRecyclerView.java:64)
                                                               at com.medum.medum.adapter.CardAdapterRecyclerView.onCreateViewHolder(CardAdapterRecyclerView.java:36)
                                                               at com.medum.medum.adapter.CardAdapterRecyclerView.onCreateViewHolder(CardAdapterRecyclerView.java:20)
                                                               at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6411)
                                                               at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5597)
                                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5482)
                                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5478)
                                                               at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2215)
                                                               at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1542)
                                                               at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1502)
                                                               at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
                                                               at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3625)
                                                               at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3354)
                                                               at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3886)
                                                               at android.view.View.layout(View.java:19393)
                                                               at android.view.ViewGroup.layout(ViewGroup.java:6022)
                                                               at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
                                                               at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
                                                               at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
                                                               at android.view.View.layout(View.java:19393)
                                                               at android.view.ViewGroup.layout(ViewGroup.java:6022)
                                                               at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
                                                               at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                                                               at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1389)
                                                               at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:868)
                                                               at android.view.View.layout(View.java:19393)
                                                               at android.view.ViewGroup.layout(ViewGroup.java:6022)
                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                               at android.view.View.layout(View.java:19393)
                                                               at android.view.ViewGroup.layout(ViewGroup.java:6022)
                                                               at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
                                                               at android.view.View.layout(View.java:19393)
                                                               at android.view.ViewGroup.layout(ViewGroup.java:6022)
                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                               at android.view.View.layout(View.java:19393)
                                                               at android.view.ViewGroup.layout(ViewGroup.java:6022)
                                                               at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
                                                               at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
                                                               at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
                                                               at android.view.View.layout(View.java:19393)
                                                               at android.view.ViewGroup.layout(ViewGroup.java:6022)
                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                               at android.view.View.layout(View.java:19393)
                                                               at android.view.ViewGroup.layout(ViewGroup.java:6022)
                                                               at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
                                                               at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
                                                               at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
                                                               at android.view.View.layout(View.java:19393)
                                                               at android.view.ViewGroup.layout(ViewGroup.java:6022)
                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
08-04 12:58:39.871 9223-9223/com.medum.medum E/AndroidRuntime:     at com.android.internal.policy.DecorView.onLayout(DecorView.java:736)
                                                               at android.view.View.layout(View.java:19393)
                                                               at android.view.ViewGroup.layout(ViewGroup.java:6022)
                                                               at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2480)
                                                               at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2199)
                                                               at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1385)
                                                               at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6722)
                                                               at android.view.Choreographer$CallbackRecord.run(Choreographer.java:886)
                                                               at android.view.Choreographer.doCallbacks(Choreographer.java:698)
                                                               at android.view.Choreographer.doFrame(Choreographer.java:633)
                                                               at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:872)
                                                               at android.os.Handler.handleCallback(Handler.java:769)
                                                               at android.os.Handler.dispatchMessage(Handler.java:98)
                                                               at android.os.Looper.loop(Looper.java:164)
                                                               at android.app.ActivityThread.main(ActivityThread.java:6540)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

适配器xml

{
    public string Text { get; set; }
    public object Value { get; set; }

    public override string ToString()
    {
        return Text;
    }
}

logcat的

ComboBoxItem item = new ComboBoxItem();
item.Text = "Item text1";
item.Value = 12;

comboBox1.Items.Add(item);

4 个答案:

答案 0 :(得分:3)

您正在投射Imageview而不是cardview尝试更改此内容:

 private CardView pictureCard;

pictureCard = (CardView ) itemView.findViewById(R.id.picturecard);

你添加了错误的id。如果只是改变这个:

pictureCard = (ImageView ) itemView.findViewById(R.id.coverImageView);

答案 1 :(得分:2)

你投错视图。这样做:

    private CardView pictureCard;

        pictureCard = (CardView) itemView.findViewById(R.id.picturecard);
在ViewHolder中

此外,此导入:

import android.widget.TextView;

在模型类中没有任何意义。

答案 2 :(得分:2)

您的ID为picturecard的视图似乎代表Cardview,但您将其视为ImageView。因此ClassCastException

我建议更换这些线 private ImageView pictureCard;pictureCard = (ImageView)itemView.findViewById(R.id.picturecard);

private CardView pictureCard;pictureCard = (CardView) itemView.findViewById(R.id.picturecard);

分别

答案 3 :(得分:0)

如果是完整的XML代码,那么我找不到结束cardview的任何标记。所以,请用

关闭你的cardview
</android.support.v7.widget.CardView>