我有这个错误,在这里读了几个关于同样事情的其他问题我还没有找到答案。 ArrayAdapter要求TextView的资源ID。我设置了这个,但由于某种原因它不起作用。任何帮助都会非常感谢。
ArrayAdapter: You must supply a resource ID for a TextView
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="100dp"
android:weightSum="1">
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="@+id/txtReceiptID"
android:text="@string/receipt_id"
android:gravity="center" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/txtReceiptURI"
android:text="@string/receipt_picture"
android:gravity="center" />
这是我的活动
listView = (ListView)findViewById(R.id.ReceiptsListView);
receiptDataAdapter = new ReceiptDataAdapter(getApplication(),R.layout.row_layout);
listView.setAdapter(receiptDataAdapter);
receiptsoperations = new ReceiptsOperations(getApplicationContext());
sqLiteDatabase = receiptsoperations.getReadableDatabase();
cursor = receiptsoperations.getReceiptsURI(sqLiteDatabase);
if(cursor.moveToFirst())
{
do {
String receiptID,receiptURI;
receiptID = cursor.getString(0);
receiptURI = cursor.getString(1);
ReceiptProvider receiptprovider = new ReceiptProvider(receiptID, receiptURI);
receiptDataAdapter.add(receiptprovider);
}while (cursor.moveToNext());
}
}
或者它可能在DataAdapter中出错了?
public class ReceiptDataAdapter extends ArrayAdapter {
List list = new ArrayList();
public ReceiptDataAdapter(Context context, int resource)
{
super(context, resource);
}
static class LayoutHandler
{
TextView RECEIPTID, RECEIPTURI;
}
@Override
public void add(Object object)
{
super.add(object);
list.add(object);
}
@Override
public int getCount(){
return list.size();
}
@Override
public Object getItem(int position){
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutHandler layoutHandler;
if(row == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.RECEIPTID = (TextView) row.findViewById(R.id.txtReceiptID);
//change this for picture rather than text
layoutHandler.RECEIPTURI = (TextView) row.findViewById(R.id.txtReceiptURI);
row.setTag(layoutHandler);
}
else
{
layoutHandler = (LayoutHandler) row.getTag();
}
ReceiptProvider receiptProvider = (ReceiptProvider) this.getItem(position);
layoutHandler.RECEIPTID.setText(receiptProvider.getReceiptID());
layoutHandler.RECEIPTURI.setText(receiptProvider.getReceiptURI());
return super.getView(position, convertView, parent);
}
}
完整的LogCat错误
You must supply a resource ID for a TextView
04-21 12:17:12.532 17135-17135/com.example.section32point5.digiceit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.section32point5.digiceit, PID: 17135
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:393)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
at com.example.section32point5.digiceit.ReceiptDataAdapter.getView(ReceiptDataAdapter.java:74)
at android.widget.AbsListView.obtainView(AbsListView.java:2421)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1280)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18893)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18893)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5966)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18893)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5966)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18893)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5966)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2693)
at android.view.View.measure(View.java:18893)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2204)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1283)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1522)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1174)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6241)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:873)
at android.view.Choreographer.doCallbacks(Choreographer.java:676)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:859)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
at com.example.section32point5.digiceit.ReceiptDataAdapter.getView(ReceiptDataAdapter.java:74)
at android.widget.AbsListView.obtainView(AbsListView.java:2421)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1280)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18893)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18893)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5966)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18893)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5966)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18893)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5966)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2693)
at android.view.View.measure(View.java:18893)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2204)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1283)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1522)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1174)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6241)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:873)
at android.view.Choreographer.doCallbacks(Choreographer.java:676)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:859)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
答案 0 :(得分:0)
您收到该错误,因为没有为显示项目提供任何资源ID。调用ReceiptDataAdapter
的超类构造函数。像
public class ReceiptDataAdapter extends ArrayAdapter {
List list = new ArrayList();
public ReceiptDataAdapter(Context context, int resource) {
super(context, resource, R.id.txtReceiptID/*resourceId in adapter item layout*/ );
}
//your code if any present
}
我认为您应该首先参考如何使用数组适配器here
编辑:
哦,伙计。没有必要再次调用超级getView
。只需返回row
视图。