2015-05-22 16 views
3

我試圖使用列表視圖列出SD卡上的所有音樂文件。而在該活動的Java代碼,我得到一個錯誤, 這裏是方法我在課堂上使用:活動文件中的符號未解決錯誤

private void init_phone_music_grid() { 
    System.gc(); 
    String[] proj = { MediaStore.Audio.Media._ID, 
      MediaStore.Audio.Media.DATA, 
      MediaStore.Audio.Media.DISPLAY_NAME, 
      MediaStore.Video.Media.SIZE }; 
    musiccursor = 
managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
      proj, null, null, null); 
    count = musiccursor.getCount(); 
    musiclist = (ListView)findViewById(R.id.sample); 
    musiclist.setAdapter(new MusicAdapter(getApplicationContext())); 

    musiclist.setOnItemClickListener(musicgridlistener); 
    mMediaPlayer = new MediaPlayer(); 
} 

現在,這裏是一個簡單的麻煩,

musiclist = (ListView)findViewById(R.id.sample); 

Android Studio中說無法解析符號sample

但是,如果我已經清楚地定義了XML,那怎麼可能呢?這裏是XML代碼:

<?xml version="1.0" encoding="utf-8"?> 

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
< ListView 
    android:id="@+id/sample" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 
</LinearLayout> 

尋找解決方案!

回答

5

不知道它是否會解決問題,但您的XML格式不正確。

不應該有任何空格<符號後:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<ListView 
    android:id="@+id/sample" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 
+0

清楚地解決了這個問題,確切的答案,這標誌着作爲答案! – OBX