我正在Kotlin进行一个Android Studio项目,我想通过MediaPlayer和SoundPool包含一些音乐和声音FX。但是,我遇到了SoundPool不能解决的问题。我将不胜感激。
当我尝试将声音加载到SoundPool时出现问题。我遵循了Android文档和一些示例。
这是我的代码:
var mediaPlayer : MediaPlayer? = null
@Suppress("DEPRECATION")
val soundPool : SoundPool = SoundPool(5,AudioManager.STREAM_MUSIC,0)
val soundIdAlarm = soundPool.load(ctx, com.example.project.R.raw.alarm, 0)
该错误是由.load(...)行引起的。我什至可以使用MediaPlayer播放声音,如以下行所示:
mediaPlayer = MediaPlayer.create(ctx,com.example.project.R.raw.alarm)
mediaPlayer?.start()
控制台输出为:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.project, PID: 12595
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.project/com.example.project.view.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:91)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:543)
at android.media.SoundPool.load(SoundPool.java:224)
at com.example.project.sound.SoundManager.<init>(SoundManager.kt:16)
at com.example.project.presenter.MainActivityPresenter.<init>(MainActivityPresenter.kt:10)
at com.example.project.view.MainActivity.<init>(MainActivity.kt:13)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at android.support.v4.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
答案 0 :(得分:0)
这些行:
at android.media.SoundPool.load(SoundPool.java:224) at com.example.project.sound.SoundManager.<init>(SoundManager.kt:16) at com.example.project.presenter.MainActivityPresenter.<init>(MainActivityPresenter.kt:10) at com.example.project.view.MainActivity.<init>(MainActivity.kt:13) at java.lang.Class.newInstance(Native Method) at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69) at android.support.v4.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43) at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
建议您正在初始化Activity
的构造函数中的代码。最早应在Activity
的{{1}}方法中进行。请参阅Understand the Activity Lifecycle上的developers.google.com文章,以更好地理解主题。