我的音乐播放器应用程序是为Android 2.3.3制作的。这是很久以前的事了,所以我的掌握现在已经生疏了。当我尝试在新的OS 4.4.4上运行时,它会崩溃。
这是我的logcat:
09-05 01:44:39.460: E/AndroidRuntime(10855): FATAL EXCEPTION: main
09-05 01:44:39.460: E/AndroidRuntime(10855): Process: com.android.finalmediaplayer, PID: 10855
09-05 01:44:39.460: E/AndroidRuntime(10855): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.finalmediaplayer/com.android.finalmediaplayer.MusicPlayer}: java.lang.NullPointerException
09-05 01:44:39.460: E/AndroidRuntime(10855): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
09-05 01:44:39.460: E/AndroidRuntime(10855): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
09-05 01:44:39.460: E/AndroidRuntime(10855): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-05 01:44:39.460: E/AndroidRuntime(10855): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-05 01:44:39.460: E/AndroidRuntime(10855): at android.os.Handler.dispatchMessage(Handler.java:102)
09-05 01:44:39.460: E/AndroidRuntime(10855): at android.os.Looper.loop(Looper.java:136)
09-05 01:44:39.460: E/AndroidRuntime(10855): at android.app.ActivityThread.main(ActivityThread.java:5001)
09-05 01:44:39.460: E/AndroidRuntime(10855): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 01:44:39.460: E/AndroidRuntime(10855): at java.lang.reflect.Method.invoke(Method.java:515)
09-05 01:44:39.460: E/AndroidRuntime(10855): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-05 01:44:39.460: E/AndroidRuntime(10855): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-05 01:44:39.460: E/AndroidRuntime(10855): at dalvik.system.NativeStart.main(Native Method)
09-05 01:44:39.460: E/AndroidRuntime(10855): Caused by: java.lang.NullPointerException
09-05 01:44:39.460: E/AndroidRuntime(10855): at com.android.finalmediaplayer.MusicPlayer.updateSongList(MusicPlayer.java:48)
09-05 01:44:39.460: E/AndroidRuntime(10855): at com.android.finalmediaplayer.MusicPlayer.onCreate(MusicPlayer.java:43)
09-05 01:44:39.460: E/AndroidRuntime(10855): at android.app.Activity.performCreate(Activity.java:5231)
09-05 01:44:39.460: E/AndroidRuntime(10855): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-05 01:44:39.460: E/AndroidRuntime(10855): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
09-05 01:44:39.460: E/AndroidRuntime(10855): ... 11 more
这是MusicPlayer.java的代码
package com.android.finalmediaplayer;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
class Mp3Filter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3"));
}
}
public class MusicPlayer extends ListActivity {
public static final String MEDIA_PATH = new String("/sdcard/");
public List<String> songs = new ArrayList<String>();
public MediaPlayer mp = new MediaPlayer();
public String selectd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songlist);
updateSongList();
}
public void updateSongList() {
File home = new File(MEDIA_PATH);
if (home.listFiles(new Mp3Filter()).length > 0) {
for (File file : home.listFiles(new Mp3Filter())) {
songs.add(file.getName());
}
ArrayAdapter<String> songList = new ArrayAdapter<String>(this,
R.layout.song_item, songs);
setListAdapter(songList);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
selectd = MEDIA_PATH + songs.get(position);
Intent nwIntent = new Intent (this, NowMusicPlay.class);
nwIntent.putExtra("ComingFrom", selectd);
nwIntent.putStringArrayListExtra("allSongs",(ArrayList<String>) songs);
nwIntent.putExtra("pos", position);
startActivityForResult(nwIntent, 0);
}
}
此代码用于扫描SD卡上的文件并列出它们。很简单,但现在它不适用于最新的Android版本。是否与新API有关?
请帮忙!如果需要,我可以提供更多代码片段。 感谢。