-2
列出文件我在Android的初學者,我想創建一個簡單的MP3播放器。但我有以下例外。我無法從Android設備
NullPointerException - 嘗試獲取空數組的長度。
使用adb外殼我驗證了那個路徑中的mp3文件存在。 這是我的代碼
活動課
public class Display extends ListActivity {
Button stop;
private static final String PATH = new String("/sdcard/");
private List<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();
private ListView mainListView ;
String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
stop = (Button) findViewById(R.id.stopbtn);
mainListView = getListView();
updatePlayList();
}
private void setButtonOnClickListeners(){
stop.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
mp.stop();
}
});}
@Override
protected void onListItemClick(ListView list, View view, int position, long id){
try{
mp.reset();
mp.setDataSource(PATH + songs.get(position));
mp.prepare();
mp.start();
}catch(IOException e){
Log.v("test", e.getMessage());
}}
private void updatePlayList() {
File home = new File(PATH);
for (File f : home.listFiles()) { // This line causes the exception
if (f.isFile())
name = f.getName();
if(name.contains(".MP3") || name.contains(".mp3")) {
ArrayAdapter<String> songList = new ArrayAdapter<String>(this, R.layout.simplerow, songs);
mainListView.setAdapter(songList);
}
}
}
}
我已經加入清單文件也讀取權限。我試着用
Environment.getExternalStorageDirectory()
也。誰能幫我?
的可能的複製[什麼是空指針異常,以及如何解決?(http://stackoverflow.com/questions/218384/what-is -a無效指針異常和如何-DO-I-FIX-IT) –
遵循本教程:http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/ –