我有一个arrayadapter,它填充了4个文本字段。在getView方法中,它尝试使用值填充文本字段。我得到错误android.content.res.Resources $ NotFoundException:字符串资源ID#0x11。你可以指导它需要纠正的地方。
以下是ArrayAdapter代码
public class AdapterTicketList extends ArrayAdapter<POJO_Ticket> {
Context context;
ArrayList<POJO_Ticket> ticketArray;
LayoutInflater vi;
public AdapterTicketList(Context context , ArrayList<POJO_Ticket> ticketArray) {
super(context, 0, ticketArray );
this.ticketArray = new ArrayList<POJO_Ticket>();
this.ticketArray.addAll(ticketArray);
this.context =context;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private class ViewHolder {
TextView tvTicketID;
TextView tvCreationHour;
TextView tvTableNumber;
TextView tvAmount;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
holder = new ViewHolder();
if (convertView == null) {
convertView = vi.inflate(R.layout.rowticket, null);
holder.tvTicketID = (TextView) convertView.findViewById(R.id.tvTicketID);
holder.tvCreationHour = (TextView) convertView.findViewById(R.id.tvCreationHour);
holder.tvTableNumber = (TextView) convertView.findViewById(R.id.tvTableNumber);
holder.tvAmount = (TextView) convertView.findViewById(R.id.tvQuantity);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Ticket ticket =ticketArray.get(position).getTicketObject();
Log.i("AdapterTicketList"," ticket id : " + ticket.getId());
holder.tvTicketID.setText(String.valueOf(ticket.getId())) ;
holder.tvCreationHour.setText(String.valueOf(ticket.getCreationHour())) ;
holder.tvTableNumber.setText(String.valueOf(ticket.getTableNumber())) ;
holder.tvAmount.setText( String.valueOf((ticket.getTotalAmount()))) ;
return convertView;
}
}
01-09 00:11:15.337: E/AndroidRuntime(10204): FATAL EXCEPTION: main
01-09 00:11:15.337: E/AndroidRuntime(10204): java.lang.NullPointerException
01-09 00:11:15.337: E/AndroidRuntime(10204): at com.example.restaurent.AdapterTicketList.getView(AdapterTicketList.java:59)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.AbsListView.obtainView(AbsListView.java:2040)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.ListView.makeAndAddView(ListView.java:1772)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.ListView.fillDown(ListView.java:672)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.ListView.fillFromTop(ListView.java:732)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.ListView.layoutChildren(ListView.java:1625)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.AbsListView.onLayout(AbsListView.java:1870)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.view.View.layout(View.java:11418)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1641)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.LinearLayout.onLayout(LinearLayout.java:1417)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.view.View.layout(View.java:11418)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1510)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.view.View.layout(View.java:11418)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.view.View.layout(View.java:11418)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1510)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.view.View.layout(View.java:11418)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.view.View.layout(View.java:11418)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.os.Looper.loop(Looper.java:137)
01-09 00:11:15.337: E/AndroidRuntime(10204): at android.app.ActivityThread.main(ActivityThread.java:4441)
01-09 00:11:15.337: E/AndroidRuntime(10204): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 00:11:15.337: E/AndroidRuntime(10204): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 00:11:15.337: E/AndroidRuntime(10204): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-09 00:11:15.337: E/AndroidRuntime(10204): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-09 00:11:15.337: E/AndroidRuntime(10204): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
这一行是正确的,因为您将数值转换为字符串:
holder.tvAmount.setText(Double.toString(ticketArray.get(position).getTicketObject().getTotalAmount())) ;
由于它们似乎在获取数值,因此存在潜在的问题:
holder.tvTicketID.setText(ticketArray.get(position).getTicketObject().getId()) ;
holder.tvCreationHour.setText(ticketArray.get(position).getTicketObject().getCreationHour()) ;
holder.tvTableNumber.setText(ticketArray.get(position).getTicketObject().getTableNumber()) ;
解决方案:必须将字符串或相应的R.string.xxx
整数传递给setText()
。
所以使用:
holder.tvTicketID.setText(String.valueOf(ticketArray.get(position).getTicketObject().getId()));
// etc
(您还应该考虑将ticketArray.get(position).getTicketObject()
保存在局部变量中,而不是重复获取它。)