2016-04-21 79 views
0

我有這個錯誤,在閱讀了關於同樣的事情,我還沒有找到答案的幾個其他問題。 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)  
+0

顯示您的ReceiptDataAdapter。 –

+0

顯示您的logcat錯誤 –

+0

感謝您的意見,我添加了LogCat錯誤和ReceiptDataAdapter。 – MrAnderson1992

回答

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視圖。

+0

它有擺脫了第一個錯誤。不幸的是,它現在說RECEIPTID是空的。 – MrAnderson1992

+0

根據你的代碼它不應該來。一旦更新你的代碼。 – shobhan

+0

不幸的是我已經閱讀過文檔,我仍然在爲此而苦苦掙扎。你能否告訴我哪裏出錯了,而不是告訴我我錯了。 – MrAnderson1992

相關問題