java.lang.IllegalStateException:在 android.os.Parcel.readException

java.lang.IllegalStateException: at android.os.Parcel.readException

我在 Android 生命体征中看到了这个异常:

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3303)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3411)
  at android.app.ActivityThread.-wrap12 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1994)
  at android.os.Handler.dispatchMessage (Handler.java:108)
  at android.os.Looper.loop (Looper.java:166)
  at android.app.ActivityThread.main (ActivityThread.java:7529)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:245)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:921)
Caused by: java.lang.IllegalStateException: 
  at android.os.Parcel.readException (Parcel.java:1962)
  at android.database.DatabaseUtils.readExceptionFromParcel (DatabaseUtils.java:183)
  at android.database.DatabaseUtils.readExceptionFromParcel (DatabaseUtils.java:135)
  at android.content.ContentProviderProxy.call (ContentProviderProxy.java:651)
  at android.provider.Settings$NameValueCache.getStringForUser (Settings.java:1996)
  at android.provider.Settings$Global.getStringForUser (Settings.java:10437)
  at android.provider.Settings$Global.getString (Settings.java:10426)
  at android.provider.Settings$Global.getInt (Settings.java:10608)
  at com.android.internal.policy.PhoneWindow.<init> (PhoneWindow.java:385)
  at com.android.internal.policy.HwPhoneWindow.<init> (HwPhoneWindow.java:91)
  at com.android.internal.policy.HwPolicyFactoryImpl.getHwPhoneWindow (HwPolicyFactoryImpl.java:49)
  at com.android.internal.policy.HwPolicyFactory.getHwPhoneWindow (HwPolicyFactory.java:50)
  at android.app.Activity.attach (Activity.java:7314)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3233)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3411)
  at android.app.ActivityThread.-wrap12 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1994)
  at android.os.Handler.dispatchMessage (Handler.java:108)
  at android.os.Looper.loop (Looper.java:166)
  at android.app.ActivityThread.main (ActivityThread.java:7529)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:245)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:921)

如何找出原因,因为它没有引用我的任何代码?

唯一的大变化是我将 .m4a 文件添加到 RAW 资源文件夹中,并将它们与 SoundPool 一起使用。我猜这与此有关,并且在间歇性地启动应用程序时崩溃。

搜索后我发现这个可以添加到我的 build.gradle 文件中:

aaptOptions {
   noCompress "mp3"
}

我添加了这个,用 mp3 代替了 m4a。还是应该是aac?也许我最好使用 .wav 文件?谢谢。

编辑: 以下说明更多,即未找到资源文件。然而大多数时候我 运行 发现应用程序没问题。

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3303)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3411)
  at android.app.ActivityThread.-wrap12 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1994)
  at android.os.Handler.dispatchMessage (Handler.java:108)
  at android.os.Looper.loop (Looper.java:166)
  at android.app.ActivityThread.main (ActivityThread.java:7529)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:245)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:921)
Caused by: android.content.res.Resources$NotFoundException: 
  at android.content.res.ResourcesImpl.openRawResourceFd (ResourcesImpl.java:380)
  at android.content.res.Resources.openRawResourceFd (Resources.java:1443)
  at android.media.SoundPool.load (SoundPool.java:235)
  at [appname].GlobalVariables.getSoundPool (GlobalVariables.java:553)
  at [appname].MainActivity.onCreate (MainActivity.java:85)
  at android.app.Activity.performCreate (Activity.java:7383)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1218)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3256)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3411)
  at android.app.ActivityThread.-wrap12 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1994)
  at android.os.Handler.dispatchMessage (Handler.java:108)
  at android.os.Looper.loop (Looper.java:166)
  at android.app.ActivityThread.main (ActivityThread.java:7529)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:245)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:921)
Caused by: java.io.FileNotFoundException: 
  at android.content.res.AssetManager.openNonAssetFdNative (AssetManager.java)
  at android.content.res.AssetManager.openNonAssetFd (AssetManager.java:803)
  at android.content.res.ResourcesImpl.openRawResourceFd (ResourcesImpl.java:378)
  at android.content.res.Resources.openRawResourceFd (Resources.java:1443)
  at android.media.SoundPool.load (SoundPool.java:235)
  at [appname].GlobalVariables.getSoundPool (GlobalVariables.java:553)
  at [appname].MainActivity.onCreate (MainActivity.java:85)
  at android.app.Activity.performCreate (Activity.java:7383)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1218)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3256)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3411)
  at android.app.ActivityThread.-wrap12 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1994)
  at android.os.Handler.dispatchMessage (Handler.java:108)
  at android.os.Looper.loop (Looper.java:166)
  at android.app.ActivityThread.main (ActivityThread.java:7529)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:245)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:921)

这似乎是一个平台错误,影响了应用安装后的第一个 运行。 OS 在提取所有 apk 内容之前启动应用程序,因此遇到缺少资源。由于外部存储的不同工作负载(取决于当时有多少进程正在使用它),它可能会间歇性地发生。您可能会注意到启动画面可绘制对象的相同行为,尤其是当它们很大时(第一次 运行 应用程序,您会在启动图像出现之前看到 white/black 屏幕更长时间 - 而设备正在解压缩文件)。

最快的解决方法是延迟资源的加载,直到它准备好(通过异常处理反复检查)。

如果这不是一个选项,您可以尝试将资源转换为资产 (java/.../res/raw -> /assets),这可能会将其推到解包队列的前面并尽快将其提供给应用程序。但是,不能保证这会(一直)有效。

最后的办法是将声音文件(如果它们不是太大)作为字节数组存储在 code/class 中(但这很难看)。这将确保数据与代码同时加载,远在执行之前。