我是android编程的初学者。 我想制作一个简单的列表视图,列出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();
}
}
这是SongsManager类
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)
这是播放列表.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" />
答案 0 :(得分:0)
显然,res/layout/playlist.xml
不包含ListView
,android:id
值为@android:id/list
。