我试图在启动画面时打开声音,但是song.start()会返回nullpointerexception。为什么会这样?我正在使用min 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();
}
Logcat:
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)
答案 0 :(得分:1)
看起来您的MediaPlayer未正确创建(来自MediaPlayer create()的文档):
返回 MediaPlayer对象,如果创建失败,则返回null
所以检查你的R.raw.splashmusic
再次来自文档:
在这种情况下,“原始”资源是系统不会尝试以任何特定方式解析的文件。但是,此资源的内容不应该是原始音频。它应该是一种支持格式的正确编码和格式化的媒体文件。
您也可以这样创建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个选择:
请详细了解MediaPlayer here
此外,在创建意图时,而不是:
Intent openMain = new Intent("com.example.hapshare.DashboardActivity");
这样做:
Intent openMain = new Intent(this, DashboardActivity.class);
答案 1 :(得分:0)
您的媒体类型错误,请检查可用的媒体类型developer.android.com/guide/appendix/media-formats.html
,因为这会导致MediaPlayer.create
返回null。