android.content.res.Resources $ NotFoundException:资源ID#0x7f07007e

时间:2018-01-09 05:04:19

标签: android android-recyclerview onclicklistener android-resources recycler-adapter

我正在尝试在应用中实现类似功能,这里是回收视图中Viewholder的片段。

Recycler customadapter

public class PostAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

TextView title,user,description,tag,time,designation;
Long date;
ImageView imageView,bookmark,profilepic,sharebutton,like;
RelativeLayout head;
LinearLayout content;
DatabaseReference reference;
Context context;
String name;

public void setContext(Context context) {
    this.context = context;
}

public PostAdapterViewHolder (final View itemView) {
    super(itemView);
    itemView.setOnClickListener(this);
    head = (RelativeLayout) itemView.findViewById(R.id.head);
    content = (LinearLayout) itemView.findViewById(R.id.content);
    imageView =(ImageView)itemView.findViewById(R.id.imageview);
    designation = (TextView) itemView.findViewById(R.id.designation);
    like =(ImageView)itemView.findViewById(R.id.like);
    profilepic = (ImageView) itemView.findViewById(R.id.imageView2);
    bookmark =(ImageView)itemView.findViewById(R.id.bookmark);
    title = (TextView) itemView.findViewById(R.id.title);
    description = (TextView) itemView.findViewById(R.id.description);
    time = (TextView) itemView.findViewById(R.id.date);
    tag = (TextView) itemView.findViewById(R.id.tag);
    sharebutton = (ImageView) itemView.findViewById(R.id.sharebutton);
    user = (TextView) itemView.findViewById(R.id.username);

    like.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ImageView i = (ImageView) itemView.findViewById(R.id.like);
            Drawable a = i.getBackground();

            if (a.getConstantState().equals(context.getDrawable(R.drawable.emptyup).getConstantState())){

                i.setImageDrawable(context.getDrawable(R.drawable.fillup));

            } else {

                i.setImageDrawable(context.getDrawable(R.drawable.emptyup));

            }

        }
    });

  }
} 

}

  

01-09 10:43:45.747 6602-6602 / ctize.connectplus.com.communitize E / AndroidRuntime:FATAL EXCEPTION:main                                                                                    流程:ctize.connectplus.com.communitize,PID:6602                                                                                    android.content.res.Resources $ NotFoundException:资源ID#0x7f07007e                                                                                        在android.content.res.Resources.getValue(Resources.java:1397)                                                                                        在android.content.res.Resources.getDrawable(Resources.java:843)                                                                                        在android.content.Context.getDrawable(Context.java:458)                                                                                        在ctize.connectplus.com.communitize.PostAdapterViewHolder $ 2.onClick(PostAdapterViewHolder.java:98)                                                                                        在android.view.View.performClick(View.java:5226)                                                                                        在android.view.View $ PerformClick.run(View.java:21350)                                                                                        在android.os.Handler.handleCallback(Handler.java:739)                                                                                        在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                        在android.os.Looper.loop(Looper.java:148)                                                                                        在android.app.ActivityThread.main(ActivityThread.java:5582)                                                                                        at java.lang.reflect.Method.invoke(Native Method)                                                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

7 个答案:

答案 0 :(得分:4)

通过查看日志。问题出在您的drawable背景中,请查看它。您有X24类型的图片更改它,它将解决您的问题。

第二件事是你一次看到两张图片。因此,您需要删除在.xml文件中设置的背景,然后就可以了。

答案 1 :(得分:2)

应显示相关活动时您的应用程序崩溃了吗?

检查您的活动中是否有像这样的可绘制v24 enter image description here

然后(如果是),在文件层次结构的最顶部将文件的显示从Android更改为Project: 从 enter image description here

enter image description here

将可绘制对象从目录drawable v-24拖放到目录drawable

仅此而已。可以的话试试

答案 2 :(得分:1)

您使用的context.getDrawable(R.drawable.fillup)仅适用于高于 LOLLIPOP 的版本。使用context.getResources().getDrawable(R.drawable.fillup)从资源中获取图像。

答案 3 :(得分:1)

<CheckBox android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 android:text="new checkbox"
 android:background="@drawable/checkbox_background" 
 android:button="@drawable/checkbox" />

尝试使用复选框而不是ImageView

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_checked="true" android:state_focused="true"
  android:drawable="@drawable/fillup" />
 <item android:state_checked="false" android:state_focused="true"
  android:drawable="@drawable/emptyup" />
 <item android:state_checked="false"
  android:drawable="@drawable/emptyup" />
 <item android:state_checked="true"
  android:drawable="@drawable/fillup" />
</selector>
  

如果   (a.getConstantState()。等于(context.getDrawable(R.drawable.emptyup).getConstantState())){

            i.setImageDrawable(context.getDrawable(R.drawable.fillup));

        } else {

            i.setImageDrawable(context.getDrawable(R.drawable.emptyup));

        }

由于你的Android版本问题,这些行给你错误。

答案 4 :(得分:0)

关注此https://developer.android.com/guide/topics/graphics/vector-drawable-resources.html#vector-drawables-backward-solution, 您应该使用setImageResource()而不是setImageDrawable()

答案 5 :(得分:0)

检查可绘制资源图像是否在 drawable 文件夹中,不是 drawable-24
我已经遇到了这个问题,但是可以通过将图像资源移动到 drawable 文件夹来解决。

答案 6 :(得分:-1)

请尝试干净的构建,可能是R文件错误