gpt4 book ai didi

java - 尝试在 My HomeFragment 中实现 RecyclerView 时应用已停止

转载 作者:行者123 更新时间:2023-11-29 06:28:23 24 4
gpt4 key购买 nike

我正在学习教程,我尝试通过添加适配器将 RecyclerView 添加到其中并在我的 HomeFragment 中显示它,但不幸的是,当我在模拟器上运行该项目时,我的应用程序停止了。

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

我的 HomeFragment.java

package com.medum.medum.view.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.medum.medum.R;
import com.medum.medum.adapter.CardAdapterRecyclerView;
import com.medum.medum.model.Picture;

import java.util.ArrayList;


public class HomeFragment extends Fragment {


public HomeFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
showtoolbar("Inicio",false,view);
RecyclerView cardsRecycler = (RecyclerView) view.findViewById(R.id.cardrecycler);

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

cardsRecycler.setLayoutManager(linearLayoutManager);

CardAdapterRecyclerView cardAdapterRecyclerView = new CardAdapterRecyclerView(buildPict(),R.layout.cardview_picture,getActivity());
cardsRecycler.setAdapter(cardAdapterRecyclerView);
return view;
}

public ArrayList<Picture> buildPict(){
ArrayList<Picture> pictures = new ArrayList<>();
pictures.add(new Picture("http://www.construyehogar.com/wp-content/uploads/2016/02/Idea-fachada-casa-dos-pisos-moderna-coralhomes.com_.au-.jpg","Casa en venta", 200000, "4 recamaras", "5"));
pictures.add(new Picture("http://weknowyourdreams.com/images/casa/casa-06.jpg","Casa en renta", 5000, "2 recamaras", "6"));
pictures.add(new Picture("http://www.terrazasdelpacifico.mx/images/departamentos_en_Tijuana_Lunada_img1.jpg","Departamento en renta", 3000, "2 recamaras", "6"));
return pictures;
}

public void showtoolbar(String tittle, boolean upButton,View view){
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(tittle);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(upButton);
}

}

我的适配器 - CardAdapterRecyclerView.java

package com.medum.medum.adapter;

import android.app.Activity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.medum.medum.R;
import com.medum.medum.model.Picture;

import java.util.ArrayList;


public class CardAdapterRecyclerView extends RecyclerView.Adapter<CardAdapterRecyclerView.CardViewHolder>{

private ArrayList<Picture> pictures;
private int resource;
private Activity activity;

public CardAdapterRecyclerView(ArrayList<Picture> pictures, int resource, Activity activity) {
this.pictures = pictures;
this.resource = resource;
this.activity = activity;
}

@Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(resource,parent,false);

return new CardViewHolder(view);
}

@Override
public void onBindViewHolder(CardViewHolder holder, int position) {
Picture picture = pictures.get(position);
holder.titlecard.setText(picture.getTitlecard());
holder.pricecard.setText(picture.getPricecard());
holder.descriptioncard.setText(picture.getDescriptioncard());
holder.timecard.setText(picture.getTimecard());

}

@Override
public int getItemCount() {
return pictures.size();
}

public class CardViewHolder extends RecyclerView.ViewHolder{

private CardView pictureCard;
private TextView titlecard;
private TextView pricecard;
private TextView descriptioncard;
private TextView timecard;
public CardViewHolder(View itemView) {
super(itemView);

pictureCard = (CardView) itemView.findViewById(R.id.picturecard);
titlecard = (TextView) itemView.findViewById(R.id.cardtitle);
pricecard = (TextView) itemView.findViewById(R.id.price);
descriptioncard = (TextView) itemView.findViewById(R.id.description);
timecard = (TextView) itemView.findViewById(R.id.timecard);
}
}
}

我的模型——Picture.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;
}
}

适配器xml

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

日志

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)

最佳答案

你正在转换 Imageview 而不是 cardview 尝试改变这个:

 private CardView pictureCard;

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

或者:

您添加了错误的 ID。如果是这样,只需更改此:

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

关于java - 尝试在 My HomeFragment 中实现 RecyclerView 时应用已停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45514896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com