2011-01-01 13 views
27

मैं एंड्रॉइड के लिए एक एप्लीकेशन कर रहा हूं और मुझे कुछ चाहिए जो यह है कि यह एसडी कार्ड में सभी फाइलों और निर्देशिकाओं की एक सूची दिखाता है और इसे विभिन्न निर्देशिकाओं के माध्यम से स्थानांतरित करने में सक्षम होना चाहिए। मुझे a good tutorial in anddev मिला। मैंने कुछ चीजों को संशोधित किया ताकि एप्लिकेशन एसडी कार्ड में चलता है न कि एंड्रॉइड रूट निर्देशिकाओं में, लेकिन शेष ज्यादातर समान होते हैं।लॉगकैट त्रुटि: "एडव्यू व्यू (व्यू, लेआउट पैराम्स) एडाप्टर व्यू में समर्थित नहीं है" एक सूची दृश्य

इस गतिविधि के लिए मेरे xml फ़ाइल है:

<?xml version="1.0" encoding="utf-8"?> 
<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@id/android:list"  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</ListView> 

और इस गतिविधि के लिए कोड है:

import hackreatorz.cifrador.R; 

import java.io.File; 
import java.util.ArrayList; 

import android.app.ListActivity; 
import android.content.res.Configuration; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class ArchivosSD extends ListActivity { 

    private ArrayList<String> directoryEntries = new ArrayList<String>(); 
    private File currentDirectory = new File("/sdcard/"); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     browseToSD(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
    } 

    private void browseToSD() { 
     browseTo(new File("/sdcard/")); 
    } 

    private void upOneLevel() { 
     if(this.currentDirectory.getParent() != null) 
      this.browseTo(this.currentDirectory.getParentFile()); 
    } 

    private void browseTo(final File directory) { 
     if (directory.isDirectory()) { 
       this.currentDirectory = directory; 
       fill(directory.listFiles()); 
     } else { 
       Toast.makeText(ArchivosSD.this, this.directoryEntries.get(this.getSelectedItemPosition()), Toast.LENGTH_SHORT).show(); 
       } 
    } 

    private void fill(File[] files) { 
     this.directoryEntries.clear(); 
     this.directoryEntries.add(getString(R.string.current_dir)); 
     if(this.currentDirectory.getParent() != null) 
      this.directoryEntries.add(getString(R.string.up_one_level)); 
      int currentPathStringLength = (int) this.currentDirectory.getAbsoluteFile().length(); 
      for (File file : files) { 
       this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLength));   
      } 
     setListAdapter(new ArrayAdapter<String>(this, R.layout.archivos_sd, this.directoryEntries)); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     int selectionRowID = (int) this.getSelectedItemPosition(); 
     String selectedFileString = this.directoryEntries.get(selectionRowID); 
     if (selectedFileString.equals(".")) { 
      this.browseToSD(); 
     } else if(selectedFileString.equals("..")) { 
      this.upOneLevel(); 
     } else { 
      File clickedFile = null; 
      clickedFile = new File(this.currentDirectory.getAbsolutePath() + this.directoryEntries.get(selectionRowID)); 
      if(clickedFile != null) 
       this.browseTo(clickedFile); 
      } 
    } 
} 

मैं ग्रहण में किसी भी त्रुटि नहीं मिलता है, लेकिन मैं एक मिल मेरे फोन पर एप्लिकेशन चलाते समय बंद करें और जब मैं लॉगकैट को देखता हूं तो मुझे निम्नलिखित दिखाई देता है:

01-01 23: 30: 29.858: त्रुटि/एंड्रॉइड रनटाइम (14 9 11): मुख्य अपवाद: मुख्य 01-01 23: 30: 29.858: त्रुटि/AndroidRuntime (14911): java.lang.UnsupportedOperationException: addView (देखें, LayoutParams) AdapterView में

मैं करने के लिए एक सुराग क्या नहीं है समर्थित नहीं है करो, मैंने Google में देखा है और मुझे कुछ भी नहीं मिला और मैंने स्टैक ओवरफ्लो पर भी ऐसा किया। जावा में और एंड्रॉइड के लिए यह मेरा पहला एप्लीकेशन है, इसलिए मैं असली एन 00 बी हूं और अगर जवाब वहां था, तो मुझे समझ में नहीं आया, इसलिए अगर आप यह समझ सकें कि मुझे इस त्रुटि को ठीक करने के लिए क्या करना चाहिए और क्यों।

अग्रिम में सबकुछ के लिए धन्यवाद।

उत्तर

14

एरेए एडाप्टर बनाते समय, ऐरेएडाप्टर को एक लेआउट संसाधन की आवश्यकता होती है जिसमें केवल एक टेक्स्ट व्यू होता है। के साथ अपने ArrayAdapter निर्माण बदलने का प्रयास करें:

setListAdapter(new ArrayAdapter<String>(this, 
     android.R.layout.simple_list_item_1, 
     this.directoryEntries)); 

करें और देखें कि यह काम करता है।

+0

बढ़िया! यह आपको बहुत धन्यवाद देता है! मुझे आपके उत्तर के बारे में कोई संदेह है, "android.R.layout.simple_list_item_1" में क्या शामिल है? अगर मैं "android.R.layout.simple_list_item_1" का उपयोग करता हूं, तो लिस्टव्यू और टेक्स्टव्यू के साथ किए गए लेआउट के साथ क्या होता है? आपका बहुत बहुत धन्यवाद! –

+0

जब आप एक ArrayAdapter बनाते हैं तो आप प्रत्येक सूची आइटम पर उपयोग किए जाने वाले लेआउट संसाधन को पास करते हैं (और ऐरेएडाप्टर केवल टेक्स्ट व्यू चाहता है)। लेआउट फ़ाइल जहां आप "स्क्रीन" सेट करते हैं, को सेटकंटेंट व्यू (R.layout.your_layout) के साथ, क्रेट के अंदर उपयोग किया जाना है। –

+0

COuld I बदलते हैं "android.R.layout.simple_list_item_1" से R.layout.myxml अगर उस XML फ़ाइल में निम्न कोड था ?: \t

58

दूसरों ने इस समस्या है और ArrayAdapter के getView में एक प्रारूप का फुलाया हैं, उनके लिए, null को parent पैरामीटर सेट में अंगूठे का view = inflater.inflate(R.layout.mylayout, null);

+3

शानदार टिप्पणी! मैं इस वजह से घंटों तक संघर्ष कर रहा हूं ... किसी को पता है क्यों? –

+0

यह मेरी समस्या भी तय करता है। मुझे लगता है कि यह सूचीदृश्य माता-पिता के साथ लेआउट पैराम्स की आवश्यकता है। – jtwigg

+12

यह निश्चित रूप से एक गलत जवाब है, आपको हमेशा एक अभिभावक दृश्य समूह को भरना चाहिए, कुछ अपवाद हैं लेकिन यह उनमें से एक नहीं है, अधिक जानकारी के लिए यहां देखें: http://www.doubleencore.com/2013/05/ लेआउट-मुद्रास्फीति के रूप में इरादा /। लोगों की सामान्य समस्या गलत तरीके से कॉन्फ़िगर की गई है xml। – Tobrun

48

नियम के रूप में

तुम हमेशा एक में पारित करने के लिए कोशिश करनी चाहिए माता-पिता एक दृश्य को फुलाते समय, अन्यथा ढांचे को उस फुलाए गए दृश्य के लिए उपयोग करने के लिए लेआउट पैराम की पहचान करने में सक्षम नहीं होगा।

अपवाद से निपटने में सही तरीका इस ध्वज को इंगित करता है, तो एक दृश्य के पारित कर दिया कंटेनर या नहीं करने के लिए सीधे जोड़ा जाना चाहिए, झूठी तीसरे पैरामीटर के रूप में गुजर रहा है।

एक एडाप्टर के मामले में, अनुकूलक ही आप getview विधि आप से देखने लौटे के बाद दृश्य जोड़ देगा:

view = inflater.inflate(R.layout.mylayout, parent, false);

BaseAdapter नमूना, just-for-us DevBytes से आता है:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View result = convertView; 
     if (result == null) { 
      result = mInflater.inflate(R.layout.grid_item, parent, false); 
     } 

     // Try to get view cache or create a new one if needed 
     ViewCache viewCache = (ViewCache) result.getTag(); 
     if (viewCache == null) { 
      viewCache = new ViewCache(result); 
      result.setTag(viewCache); 
     } 

     // Fetch item 
     Coupon coupon = getItem(position); 

     // Bind the data 
     viewCache.mTitleView.setText(coupon.mTitle); 
     viewCache.mSubtitleView.setText(coupon.mSubtitle); 
     viewCache.mImageView.setImageURI(coupon.mImageUri); 

     return result; 
    } 

कर्सर एडाप्टर नमूना:

@Override 
    public View newView(final Context context, final Cursor cursor, final ViewGroup root)   { 
     return mInflater.inflate(R.layout.list_item_ticket, root, false); 
    } 

देखें मुद्रास्फीति के बारे में अधिक जानकारी इस article में मिल सकती है।

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