package com.example.papbiensaude;
import android.graphics.drawable.Drawable;
import android.view.Choreographer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
public class SimpleItem extends DrawerItem<SimpleItem.ViewHolder> {
private int selectedItemIconTint;
private int selectedItemTextTint;
private int normalItemIconTint;
private int normalItemTextTint;
private Drawable icon;
private String title;
public SimpleItem(Drawable icon, String title){
this.icon = icon;
this.title = title;
}
@Override
public ViewHolder createViewHolder(ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.item_option,parent,false);
return new ViewHolder(v);
}
@Override
public void bindViewHolder(ViewHolder holder) {
holder.title.setText(title);
holder.icon.setImageDrawable(icon);
holder.title.setText(isChecked ? selectedItemTextTint : normalItemTextTint);
holder.icon.setColorFilter(isChecked ? selectedItemIconTint : normalItemIconTint);
}
public SimpleItem withSelectedIconTint(int selectedItemIconTint){
this.selectedItemIconTint = selectedItemIconTint;
return this;
}
public SimpleItem withSelectedTextTint(int selectedItemTextTint){
this.selectedItemTextTint = selectedItemTextTint;
return this;
}
public SimpleItem withIconTint(int normalItemIconTint){
this.normalItemIconTint = normalItemIconTint;
return this;
}
public SimpleItem withTextTint(int normalItemTextTint){
this.normalItemTextTint = normalItemTextTint;
return this;
}
public class ViewHolder extends DrawerAdapter.ViewHolder{
private ImageView icon;
private TextView title;
public ViewHolder(@NonNull View itemView) {
super(itemView);
icon = itemView.findViewById(R.id.icon);
title = itemView.findViewById(R.id.title);
}
}
}
package com.example.papbiensaude;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.ViewHolder> {
private List<DrawerItem> items;
private Map<Class<? extends DrawerItem>, Integer> viewTypes;
private SparseArray<DrawerItem> holderFactories;
private OnItemSelectedListener listener;
public DrawerAdapter( List<DrawerItem> items){
this.items = items ;
this.viewTypes = new HashMap<>();
this.holderFactories = new SparseArray<>();
processViewTypes();
}
private void processViewTypes() {
int type = 0;
for (DrawerItem item : items){
if(!viewTypes.containsKey(item.getClass())){
viewTypes.put(item.getClass(),type);
holderFactories.put(type, item);
type++;
}
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ViewHolder holder = holderFactories.get(viewType).createViewHolder(parent);
holder.drawerAdapter = this;
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
items.get(position).bindViewHolder(holder);
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public int getItemViewType(int position){
return viewTypes.get(items.get(position).getClass());
}
public void setSelected(int position){
DrawerItem newChecked = items.get(position);
if (!newChecked.isSelectable()){
return;
}
for (int i=0; i<items.size(); i++){
DrawerItem item = items.get(i);
if (item.isChecked()){
item.setChecked(false);
notifyItemChanged(i);
break;
}
}
newChecked.setChecked(true);
notifyItemChanged(position);
if (listener != null){
listener.onItemSelected(position);
}
}
public void setListener(OnItemSelectedListener listener){
this.listener = listener;
}
public interface OnItemSelectedListener{
void onItemSelected(int position);
}
static abstract class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private DrawerAdapter drawerAdapter;
public ViewHolder (@NonNull View itemView){
super(itemView);
itemView.setOnClickListener(this);
}
public void onClick(View v){
drawerAdapter.setSelected(getAdapterPosition());
}
}
}
android.content.res.Resources$NotFoundException: String resource ID #0xff000000
at android.content.res.Resources.getText(Resources.java:338)
at android.widget.TextView.setText(TextView.java:5494)
at com.example.papbiensaude.SimpleItem.bindViewHolder(SimpleItem.java:42)
at com.example.papbiensaude.SimpleItem.bindViewHolder(SimpleItem.java:14)
at com.example.papbiensaude.DrawerAdapter.onBindViewHolder(DrawerAdapter.java:52)
at com.example.papbiensaude.DrawerAdapter.onBindViewHolder(DrawerAdapter.java:16)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3540)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:146)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:721)
at android.view.View.measure(View.java:22002)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1498)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
2021-02-04 17:12:28.114 15544-15544/com.example.papbiensaude E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
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)
我仍然不明白错误在哪里,如何恢复它!
我想问题出在这里:
holder.title.setText(isChecked ? selectedItemTextTint : normalItemTextTint);
settext(int)
方法需要一个字符串资源标识符,类似于r.string.my_string
。您可能(意外地)向它传递了一个颜色int:0xff000000
(不透明的黑色)。
或者将settext()
更改为settextcolor()
,或者将参数更改为字符串资源ID。
问题内容: 我正在开发一个Android应用程序,该程序从MySQL数据库读取数据,但遇到了此错误。我有这个XML布局: 这是我的Java文件: 调用此Activity时,我收到以下错误消息: 我不知道如何解决此错误。 问题答案: 更改 至 有不同的版本-一种采用字符串,一种采用int资源ID。如果将其传递给整数,它将尝试查找相应的字符串资源ID-找不到它,这是你的错误。 我想返回一个整数。你需要
我正在开发一个从MySQL数据库读取数据的Android应用程序,我遇到了这个错误。我有这样的XML布局: 这是我的Java文件: 当调用此activity时,我收到以下错误消息: 我不知道如何修复这个错误。
回收器自定义适配器 01-09 10:43:45.747 660 2-6602/Ctize.connectplus.com.communitize E/AndroidRuntime:致命异常:主进程:Ctize.connectplus.com.communitize,PID:6602 Android.content.res.resources$NotFoundException:资源ID#0x7F
问题内容: 运行片段尝试显示对象的ListView时会发生此异常。我已经研究了该异常,但针对其他情况的解决方案似乎与“ setText”函数有关,或者其他任何尝试将除字符串以外的任何内容显示为字符串的问题。我真的不确定还有什么要说的。提前致谢! Java类 } XML布局 编辑(日志) 问题答案: 发现您的错误。它在您的IngAdapter中的以下行中 在此,第二个参数应该是资源名称,并且绝不能使