我有這個錯誤,在閱讀了關於同樣的事情,我還沒有找到答案的幾個其他問題。 ArrayAdapter正在爲TextView請求一個資源ID。我已經設置了這個,但由於某種原因它不起作用。任何幫助將非常感謝。錯誤「ArrayAdapter:你必須提供一個TextView的資源ID」,list_layout.xml
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)
顯示您的ReceiptDataAdapter。 –
顯示您的logcat錯誤 –
感謝您的意見,我添加了LogCat錯誤和ReceiptDataAdapter。 – MrAnderson1992