我正在尝试开发简单的mp3文件。显示的mp3文件已填入列表中。我的代码是:
class Mp3Filter implements FilenameFilter{
@Override
public boolean accept(File dir, String name)
{
return (name.endsWith(".mp3"));
}
}
public class MyActivity extends ListActivity {
private String SD_PATH = new String("/sdcard/Music");
private List<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
updatePlaylist();
Button stopPlay= (Button) findViewById(R.id.stopBtn);
stopPlay.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
mp.stop();
}
});
}
private void updatePlaylist() {
String TAG="CHUSS";
File home = new File("SD_PATH");
if(home.listFiles( new Mp3Filter()).length>0){
for(File file : home.listFiles( new Mp3Filter())){
songs.add(file.getName());
Log.e(TAG,file.getName());
}
ArrayAdapter<String> songlist = new ArrayAdapter<String> (this,R.layout.songs_item,songs);
setListAdapter(songlist);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
代码中的错误
3-05 19:44:46.807 4034-4034/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.hawk.player4, PID: 4034
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hawk.player4/com.example.hawk.player4.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)