android.content.res.$NotFoundException:字符串资源ID #0



我的抽屉有问题。我用的是碎片。如果我试着运行"应用程序",它就会停止。出现错误。我计算出问题是bindViewHolder,但我不知道如何解决它…

In my SimpleItem.java

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);
}
}
}

In my DrawerAdapter.java

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());
}
}
}

LOGCAT: E/AndroidRuntime: FATAL EXCEPTION: main

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。

相关内容

  • 没有找到相关文章

最新更新