我正在创建一个显示我的食物清单的应用。我想通过回收者视图在主要活动中显示列表,但是出现错误,我尝试运行调试器,但是它不断跳入系统类,我在偏好设置-调试器-步进中添加了android。*,但它不起作用。这是我第一次从事自己的项目。感谢您的回答
这是我的适配器
public class CookingAdapter extends RecyclerView.Adapter<CookingAdapter.Viewholder>{
ArrayList<Food> foodList= new ArrayList<>();
private String name;
private int imageID;
public CookingAdapter(ArrayList<Food>foods) {
foodList=foods;
}
public static class Viewholder extends RecyclerView.ViewHolder{
CardView cardView;
public Viewholder(@NonNull CardView itemView) {
super(itemView);
cardView= itemView;
}
}
@NonNull
@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
CardView cv =(CardView) LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.layout_cardview,viewGroup,false);
return new Viewholder(cv);
}
@Override
public void onBindViewHolder(@NonNull Viewholder viewholder, final int position) {
CardView cv = viewholder.cardView;
ImageView imageView = (ImageView)cv.findViewById(R.id.info_image);
Drawable drawable = ContextCompat.getDrawable(cv.getContext(),
foodList.get(position).getImageId());
imageView.setImageDrawable(drawable);
imageView.setContentDescription(foodList.get(position).getName());
TextView textView = (TextView)cv.findViewById(R.id.info_text);
textView.setText(foodList.get(position).getName());
}
@Override
public int getItemCount() {
return foodList.size();
}
食物课
public class Food {
private String name;
private String recipe;
private int imageId;
private int favorite;
public Food(String name, String recipe, int imageId, int favorite) {
this.name = name;
this.recipe = recipe;
this.imageId = imageId;
this.favorite = favorite;
}
public Food(ArrayList<Food>foods, final int position) {
this.name = foods.get(position).getName();
this.recipe = foods.get(position).getRecipe();
this.imageId = foods.get(position).getImageId();
this.favorite = foods.get(position).getFavorite();
}
public String getName() {
return name;
}
public String getRecipe() {
return recipe;
}
public int getImageId() {
return imageId;
}
public int getFavorite() {
return favorite;
}
} 数据库
public class CookingDatabase extends SQLiteOpenHelper {
private static final String DB_NAME="Cooking";
private static final int DB_VERSION = 1;
private ArrayList<Food>foods = new ArrayList<>();
// Database Constructor
public CookingDatabase(Context context) {
super(context, DB_NAME,null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
updateDatabase(db,0,DB_VERSION);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private void insert(SQLiteDatabase db,String name,String recipe, int imageID, int favortie)
{
ContentValues cookingValue = new ContentValues();
cookingValue.put("NAME",name);
cookingValue.put("RECIPE",recipe);
cookingValue.put("IMAGE_ID",imageID);
cookingValue.put("FAVORITE",favortie);
db.insert(DB_NAME,null,cookingValue);
}
private void updateDatabase(SQLiteDatabase db,int oldVer, int newVer){
String table="CREATE TABLE COOKING"+"(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
" "+" NAME TEXT, "+" RECIPE TEXT, "+"IMAGE_ID INTEGER,"+"FAVORITE INTEGER)";
if(oldVer < 1){
db.execSQL(table);
insert(db,"Pizza","Bake in 10 minutes",R.drawable.pizza,0);
insert(db,"Steak","Salt and pepper",R.drawable.steak,0);
insert(db,"Sushi","Fresh Salmon",R.drawable.sushi,0);
}
}
public ArrayList<Food> getData(){
SQLiteDatabase db;
String row="select * from " + DB_NAME;
db= this.getReadableDatabase();
Cursor cursor = db.rawQuery(row,null);
if(cursor != null){
if(cursor.moveToFirst()){
do {
foods.add(new Food(cursor.getString(cursor.getColumnIndex("NAME")),
cursor.getString(cursor.getColumnIndex("RECIPE")),
cursor.getInt(2),cursor.getInt(3)));
}while (cursor.moveToNext());
}
}
cursor.close();
db.close();
return foods;
}
public ArrayList<Food> getFoods() {
return foods;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
CookingDatabase db= new CookingDatabase(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView recyclerView=(RecyclerView)findViewById(R.id.id_recycler);
CookingAdapter adapter = new CookingAdapter(db.getData());
recyclerView.setAdapter(adapter);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);
recyclerView.setLayoutManager(gridLayoutManager);
}
} MainActivyti XML
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="hfad.com.mycooking.MainActivity">
<include
layout="@layout/toolbar_main"
android:id="@+id/toolbar"/>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pizza_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" /></LinearLayout>
Logcat
2019-02-09 16:19:14.238 3503-3503/hfad.com.mycooking E/AndroidRuntime: FATAL EXCEPTION: main
Process: hfad.com.mycooking, PID: 3503
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
at android.content.res.Resources.getDrawable(Resources.java:766)
at android.content.Context.getDrawable(Context.java:525)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:463)
at hfad.com.mycooking.CookingAdapter.onBindViewHolder(CookingAdapter.java:45)
at hfad.com.mycooking.CookingAdapter.onBindViewHolder(CookingAdapter.java:17)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
2019-02-09 16:19:14.238 3503-3503/hfad.com.mycooking E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
答案 0 :(得分:0)
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
at android.content.res.Resources.getDrawable(Resources.java:766)
at android.content.Context.getDrawable(Context.java:525)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:463)
at hfad.com.mycooking.CookingAdapter.onBindViewHolder(CookingAdapter.java:45)
at hfad.com.mycooking.CookingAdapter.onBindViewHolder(CookingAdapter.java:17)
这些行表明您的Viewholder
在绑定视图时找不到资源:
@Override
public void onBindViewHolder(@NonNull Viewholder viewholder, final int position) {
CardView cv = viewholder.cardView;
ImageView imageView = (ImageView)cv.findViewById(R.id.info_image);
Drawable drawable = ContextCompat.getDrawable(cv.getContext(),
foodList.get(position).getImageId());
imageView.setImageDrawable(drawable);
imageView.setContentDescription(foodList.get(position).getName());
TextView textView = (TextView)cv.findViewById(R.id.info_text);
textView.setText(foodList.get(position).getName());
}
特别是,我的猜测是问题出在这里:
Drawable drawable = ContextCompat.getDrawable(cv.getContext(),
foodList.get(position).getImageId());
imageView.setImageDrawable(drawable);
确保传递正确的资源ID。注意:存储资源ID并不是一个好主意
(R.drawable.example)
作为类字段
(foodList.get(position).getImageId())
用不同的资源映射它们,例如。带有枚举类:
public enum FoodType {
PIZZA(R.drawable.pizza),
BURGER(R.drawable.burger);
private Integer drawableRes;
private FoodType(Integer drawableRes){
this.drawableRes = drawableRes;
}
public Integer getDrawableRes() {
return drawableRes;
}
}
然后,检查类型并按以下方式获取它:
FoodType type;
if(type==FoodType.BURGER){
type.getDrawableRes();
}