2014-05-07 62 views
1

我有一個關於使用微調器和適配器的問題,我想搜索目錄中的所有文件,並將文件的名稱放在微調器中。 這是我的代碼:一個微調器中的ANDROID適配器

File repertoire = new File(Environment.getExternalStorageDirectory()+"/androidtest/vente") ; 


    String[] tabFile = repertoire.list(); 

    if (tabFile==null){ 

     Log.d("file null","testdfed"); 
    } 
    Log.d("//","///////////"); 
    for (int i = 0; i < tabFile.length; i++) { 
     Log.d("Tableau",tabFile[i].toString()); 
    } 

    ArrayList<String> list = new ArrayList<String>(); 
    for(int i=0; i<tabFile.length; i++) { 
     list.add(tabFile[i]); 

    } 

//for checking in the logcat if my code have found all the files   
    for (String s : list) { 

     Log.d("Liste",s); 
    } 


//creation of the adapter 
    ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, 
      android.R.layout.simple_spinner_item, list); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    sp.setAdapter(adapter); 

//finally the listener 
    sp.setOnItemSelectedListener(new OnItemSelectedListener() { 

    public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) { 
     int item = sp.getSelectedItemPosition(); 
     Toast.makeText(getBaseContext(), 
       "You have selected 222 : " + item, 
       Toast.LENGTH_SHORT).show(); 

    } 
    public void onNothingSelected(AdapterView<?> arg0) { } 
}); 

我在sp.setAdapter(適配器)代碼崩潰; 謝謝! :)

我在SOF新的SSO我不得不修改我的文字: 對於穆克什庫馬爾 已經做好的答案,我初始化的onCreate

public class OfflineActivity extends FragmentActivity { 

private PagerAdapter mPagerAdapter; 
private Spinner sp = (Spinner)findViewById(R.id.spinm); 

protected void onCreate(Bundle savedInstanceState) { 

,所以我代替,就像您

public class OfflineActivity extends FragmentActivity { 

private PagerAdapter mPagerAdapter; 
private Spinner sp; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    super.setContentView(R.layout.viewpager); 
    sp = (Spinner)findViewById(R.id.spinm); 

BU我的代碼崩潰出於同樣的原因:/

+1

更換ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, list);

請出示錯誤日誌 –

+0

發佈logcat錯誤消息。 –

+0

05-07 12:00:03.731:E/Trace(6236):打開跟蹤文件時出錯:沒有這樣的文件或目錄(2) – Lockon

回答

0

當你告訴My code crash at sp.setAdapter(adapter);

可能你還沒有初始化sp,。首先將其初始化,然後使用。即 在您的ActivityonCreate()中寫下這段代碼。

sp= (Spinner)findViewById(R.id.Spinner_id_in_your_xml); 
0

這將適用於一個微調

spinner1 = (Spinner) findViewById(R.id.spinner1); 
    List<String> list = new ArrayList<String>(); 
    list.add("Android"); 
    list.add("Java"); 
    list.add("Spinner Data"); 
    list.add("Spinner Adapter"); 
    list.add("Spinner Example"); 

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list); 
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner1.setAdapter(dataAdapter); 
+0

同樣的方法,這段代碼不會爲我崩潰如果我評論: // spinner1 .setAdapter(DataAdapter的); – Lockon

0

這可能是因爲您傳遞的片段,而不是背景下試圖用

ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActivity(), 
     android.R.layout.simple_spinner_item, list); 
+0

另一個錯誤:getActivity()在類OfflineActivity中未定義//我的主類 – Lockon

相關問題