我正在使用RecyclerView来显示数据。但是,当我尝试在onBindViewHolder()方法中绑定数据时,我收到以下错误...
09-12 16:37:51.217 2166-2166/com.zaptech.databasedemowithrecycler E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x17
at android.content.res.Resources.getText(Resources.java:230)
at android.widget.TextView.setText(TextView.java:3769)
at com.zaptech.databasedemowithrecycler.adapter.MyEmployeeAdapter.onBindViewHolder(MyEmployeeAdapter.java:47)
at com.zaptech.databasedemowithrecycler.adapter.MyEmployeeAdapter.onBindViewHolder(MyEmployeeAdapter.java:24)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5277)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5310)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4568)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1371)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
我的适配器的代码如下,其中还包括ViewHolder类:
public class MyEmployeeAdapter extends RecyclerView.Adapter<MyEmployeeAdapter.EmployeeViewHolder> {
private List<ModelEmployee> empItemList;
private Context mContext;
public MyEmployeeAdapter(Context context, List<ModelEmployee> feedItemList) {
this.empItemList = feedItemList;
this.mContext = context;
}
@Override
public EmployeeViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.rowlayout_emp, null);
EmployeeViewHolder viewHolder = new EmployeeViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(EmployeeViewHolder holder, int position) {
ModelEmployee feedItem = empItemList.get(position);
Log.d("NAME : ", feedItem.getName());
Log.d("AGE : ", String.valueOf(feedItem.getAge()));
holder.txtName.setText(feedItem.getName());
holder.txtAge.setText(feedItem.getAge());
}
@Override
public int getItemCount() {
return (null != empItemList ? empItemList.size() : 0);
}
public class EmployeeViewHolder extends RecyclerView.ViewHolder {
public TextView txtName;
public TextView txtAge;
public EmployeeViewHolder(View view) {
super(view);
this.txtName = (TextView) view.findViewById(R.id.name);
this.txtAge = (TextView) view.findViewById(R.id.age);
}
}
}
坚持以下几行:
@Override
public void onBindViewHolder(EmployeeViewHolder holder, int position) {
ModelEmployee feedItem = empItemList.get(position);
Log.d("NAME : ", feedItem.getName());
Log.d("AGE : ", String.valueOf(feedItem.getAge()));
holder.txtName.setText(feedItem.getName());
holder.txtAge.setText(feedItem.getAge());
}
拜托,让我知道错误的事情发生在我身上......
答案 0 :(得分:1)
问题是android将您的feedItem.getAge()
解释为资源ID并尝试从string.xml
获取相应的字符串资源。
因此抛出了运行时异常(找不到资源),而不是编译器错误。