我是Android编程的初学者。我想制作一个简单的listView,列出了从SD卡中列出所有MP3文件。我尝试了很多示例代码,但并非所有这些代码都完全工作。
的帮助将被应用。public class PlayListActivity extends Activity {
private ListView lv;
//Songs List
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
SimpleAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
lv = (ListView) findViewById(android.R.id.list);
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
//ArrayList<String> songsListData = new ArrayList<>();
SongsManager plm = new SongsManager();
this.songsList = plm.getPlayList();
for(int i=0; i < songsList.size();i++) {
HashMap<String, String> song = songsList.get(i);
songsListData.add(song);
}
ListAdapter adapter = new SimpleAdapter(this, songsListData,
R.layout.playlist_item, new String [] {"songTitle"},new int[] {
R.id.songTitle});
//setListAdapter(adapter);
lv.setAdapter(adapter);
//ListView lv = getListView();
}
}
这是Songnmanager类
public class SongsManager {
//SDCARD
final String MEDIA_PATH = Environment.getExternalStorageDirectory().getPath() + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern=".mp3";
//Constructor
public SongsManager(){
}
public ArrayList<HashMap<String, String >> getPlayList(){
System.out.println(MEDIA_PATH);
if (MEDIA_PATH !=null){
File home = new File(MEDIA_PATH);
File[] listFiles = home.listFiles();
if(listFiles != null && listFiles.length>0){
for(File file:listFiles){
System.out.println(file.getAbsolutePath());
if (file.isDirectory()){
scanDirectory(file);
}else{
addSongToList(file);
}
}
}
}
return songsList;
}
private void scanDirectory(File directory) {
if(directory != null){
File[]listFiles = directory.listFiles();
if(listFiles != null && listFiles.length>0){
for (File file : listFiles){
if(file.isDirectory()){
scanDirectory(file);
}else{
addSongToList(file);
}
}
}
}
}
private void addSongToList(File song) {
if(song.getName().endsWith(mp3Pattern)){
HashMap<String, String> songMap = new HashMap<String ,String>();
songMap.put("songTitle",song.getName().substring(0,(song.getName().length()-4)));
songMap.put("songPath",song.getPath());
songsList.add(songMap);
}
}
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}
它给出错误消息:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a45v.praransemen/com.example.a45v.praransemen.PlayListActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.a45v.praransemen.PlayListActivity.onCreate(PlayListActivity.java:41)
at android.app.Activity.performCreate(Activity.java:6323)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是playlist.xml
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
这是playlist_item.xml
<TextView
android:id="@+id/songTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
显然, res/layout/playlist.xml
不包含 android:id
值@android:id/list
的ListView
。