我试图在启动屏幕打开时发出声音,但 Song.start() 返回空指针异常。为什么会出现这种情况呢?我使用的是最小 API 11。
代码:
public class Splash extends Activity{
MediaPlayer song;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bg);
song = MediaPlayer.create(Splash.this, R.raw.splashmusic);
song.start();
Thread timer = new Thread(){//create thread to execute one class to another class within a time
public void run(){
try{
sleep(5000);//5 seconds of pausing
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openMain = new Intent("com.example.hapshare.DashboardActivity");
startActivity(openMain);
}
}
};
timer.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
song.release();
finish();
}
日志猫:
11-19 09:42:02.631: E/AndroidRuntime(20289): FATAL EXCEPTION: main
11-19 09:42:02.631: E/AndroidRuntime(20289): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hapshare/com.example.hapshare.Splash}: java.lang.NullPointerException
11-19 09:42:02.631: E/AndroidRuntime(20289): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2517)
11-19 09:42:02.631: E/AndroidRuntime(20289): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
11-19 09:42:02.631: E/AndroidRuntime(20289): at android.app.ActivityThread.access$600(ActivityThread.java:162)
11-19 09:42:02.631: E/AndroidRuntime(20289): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
11-19 09:42:02.631: E/AndroidRuntime(20289): at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 09:42:02.631: E/AndroidRuntime(20289): at android.os.Looper.loop(Looper.java:158)
11-19 09:42:02.631: E/AndroidRuntime(20289): at android.app.ActivityThread.main(ActivityThread.java:5789)
11-19 09:42:02.631: E/AndroidRuntime(20289): at java.lang.reflect.Method.invokeNative(Native Method)
最佳答案
看起来您的 MediaPlayer 未正确创建(来自 MediaPlayer create() 的文档):
Returns a MediaPlayer object, or null if creation failed
所以检查一下你的 R.raw.splashmusic
再次来自文档:
In this case, a "raw" resource is a file that the system does not try to parse in any particular way. However, the content of this resource should not be raw audio. It should be a properly encoded and formatted media file in one of the supported formats.
您还可以通过这种方式创建 MediaPlayer 对象:
try {
AssetFileDescriptor afd = this.getResources().openRawResourceFd(R.raw.splashmusic);
song = new MediaPlayer();
song.reset();
song.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
song.setAudioStreamType(AudioManager.STREAM_MUSIC);
song.prepare(); // might take long! (for buffering, etc)
song.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
但请记住,prepare() 可能需要很长时间,因此在 UI 线程上创建 MediaPlayer 并不是一个好主意。您这里有 2 个选择:
- 创建另一个线程并在那里生成 MP
- 使用prepareAsync(),准备完成后,调用通过setOnPreparedListener()配置的MediaPlayer.OnPreparedListener的onPrepared()方法。
请阅读有关 MediaPlayer 的更多信息 here
此外,在创建 Intent 时,而不是:
Intent openMain = new Intent("com.example.hapshare.DashboardActivity");
这样做:
Intent openMain = new Intent(this, DashboardActivity.class);
关于java - Android - 无法在启动屏幕上播放歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20069705/