【问题标题】:android.content.res.Resources$NotFoundException: Resource ID #0x7f07007eandroid.content.res.Resources$NotFoundException:资源 ID #0x7f07007e
【发布时间】:2018-06-18 02:32:24
【问题描述】:

我正在尝试在应用程序中实现类似的功能,这是回收视图中 Viewholder 的 sn-p。

回收站自定义适配器

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: 致命异常: 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) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

【问题讨论】:

  • 共享整个代码...
  • 发布您的错误日志
  • 从您的日志看来问题出在可绘制对象上。
  • public class Example extends RecyclerView.Adapter{ } // 实现所有覆盖函数并执行 onCreateViewHolder 查找布局和 onBindViewHolder 分配所有值并创建扩展 RecyclerView.ViewHolder 的内部类和在那个实现它的方法,在那个地方你找到所有的 id`s。
  • @GauravRai 我相信您还在 xml 文件中将背景可绘制设置为您的 imageView。检查那里,如果没有,则清理并重建您的项目并再次运行。

标签: android onclicklistener android-resources android-recyclerview


【解决方案1】:

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

检查您的活动中是否有这样的可绘制 v24

然后,如果是,请将文件的显示从 Android 更改为文件层次结构最顶端的 Project: 从

并将drawable v-24目录中的drawable拖放到drawable目录中。

就是这样。试试就好了

编辑:我发现问题可能出现在 Android 6 API 23 但不是更高的 API

【讨论】:

  • 谢谢先生,这解决了我在使用 TabLayout 时遇到的问题。当我将 v24 可绘制对象移动到另一个文件夹时,它可以正常工作
【解决方案2】:

通过查看日志。问题出在你的drawable 后台看看。你有输入X24图像改变它,它会解决你的问题。

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 当然我会记住这一点,对代码进行必要的更改。
    【解决方案4】:
    <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().equals(context.getDrawable(R.drawable.emptyup).getConstantState())){

                i.setImageDrawable(context.getDrawable(R.drawable.fillup));
    
            } else {
    
                i.setImageDrawable(context.getDrawable(R.drawable.emptyup));
    
            }
    

    由于您的 android 版本问题,这些行给您错误。

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多