2009-12-10 11 views
21

मैं पॉप्युलेट करने के लिए एक MatrixCursor का उपयोग कर समस्या हो रही है के साथ एक ListView में MatrixCursor और SimpleCursorAdapter का उपयोग करते हुए मेरी ListView:पाठ और छवियों

private void fillData() { 
    String[] menuCols = new String[] { "icon", "item", "price" }; 
    int[] to = new int[] { R.id.icon, R.id.item, R.id.price }; 

    MatrixCursor menuCursor = new MatrixCursor(menuCols); 
    startManagingCursor(menuCursor); 

    menuCursor.addRow(new Object[] { R.drawable.chicken_sandwich, "Chicken Sandwich", "$3.99" }); 

    SimpleCursorAdapter menuItems = new SimpleCursorAdapter(
      this, R.layout.menu_row, menuCursor, menuCols, to); 

    setListAdapter(menuItems); 
} 

निर्माण SimpleCursorAdapter एक दुर्घटना का कारण बनता है। यहां तक ​​कि जब मैंने आइकन को हटाने का प्रयास किया तब भी ऐप क्रैश हो गया। है क्रैश के समय की कॉल स्टैक यहाँ: यहाँ मेरी menu_row.xml है:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    </ImageView> 
    <TextView 
     android:id="@+id/item" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    </TextView> 
    <TextView 
     android:id="@+id/price" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    </TextView> 
</LinearLayout> 

संपादित

Thread [<3> main] (Suspended (exception RuntimeException)) 
    ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2481 
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2497 
    ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 119 
    ActivityThread$H.handleMessage(Message) line: 1848 
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4338  
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] 
    Method.invoke(Object, Object...) line: 521 
    ZygoteInit$MethodAndArgsCaller.run() line: 860 
    ZygoteInit.main(String[]) line: 618 
    NativeStart.main(String[]) line: not available [native method] 

समाधान:

मैं समस्या और समाधान नीचे मेरे जवाब में है।

+3

जब आप दुर्घटनाग्रस्त हो जाते हैं, तो आप कहां गलत हो रहे हैं, यह पता लगाने में सहायता के लिए स्टैक ट्रेस देख सकते हैं - ग्रहण में एडीबी लॉगकैट, डीडीएमएस, या डीडीएमएस परिप्रेक्ष्य का उपयोग करें। यदि इससे आपको समस्या को ठीक करने में पर्याप्त सहायता नहीं मिलती है, तो स्टैक ट्रेस को अपने प्रश्न के संपादन के रूप में पोस्ट करें, क्योंकि इससे हमें कुछ संकेत मिल सकते हैं। – CommonsWare

+0

उत्कृष्ट सवाल ... मेरी इच्छा है कि मुझे यह दो हफ्ते पहले मिले! नमूना कोड के लिए धन्यवाद। बहुत बढ़िया!! – mobibob

उत्तर

20

चलो इस ग्रह को एक्लिप्स का उपयोग करके जावा डिबगिंग के साथ अनुभवहीनता के लिए चॉकलेट करें।

डीबगर में एप्लिकेशन चलाते हुए, मैंने एक रनटाइम अपवाद के साथ दुर्घटनाग्रस्त हो गया। कॉल स्टैक में बहुत ही शीर्ष तत्व पर क्लिक करने से मुझे वैरिएबल की सूची मिली, जिस पर मैंने अपना अपवाद ई देखा।

विशिष्ट त्रुटि एक अवैध अमान्य था क्योंकि मेरे मैट्रिक्स कर्सर में _id कॉलम नहीं था। _id लेबल वाले कॉलम को जोड़ने से समस्या ठीक हो गई है और अब सबकुछ काम करता है।

मुझे फिर से डीबगर देखने के लिए धन्यवाद! अपने उपकरणों के बारे में सहज और जानकार रहें!

संबंधित मुद्दे