我有一个非常简单的应用程序,只是一个按钮,它可以播放声音并在单击时更改图像。
我正在获取崩溃报告,但我不知道是什么原因导致崩溃,是声音还是图像?:
java.lang.OutOfMemoryError:
at dalvik.system.VMRuntime.newNonMovableArray (Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset (Native Method)
at android.graphics.BitmapFactory.decodeStream (BitmapFactory.java:620)
at android.graphics.BitmapFactory.decodeResourceStream (BitmapFactory.java:455)
at android.graphics.drawable.Drawable.createFromResourceStream (Drawable.java:1152)
at android.content.res.ResourcesImpl.loadDrawableForCookie (ResourcesImpl.java:859)
at android.content.res.ResourcesImpl.loadDrawable (ResourcesImpl.java:710)
at android.content.res.Resources.loadDrawable (Resources.java:863)
at android.content.res.TypedArray.getDrawable (TypedArray.java:928)
at android.graphics.drawable.StateListDrawable.inflateChildElements (StateListDrawable.java:171)
at android.graphics.drawable.StateListDrawable.inflate (StateListDrawable.java:116)
at android.graphics.drawable.DrawableInflater.inflateFromXml (DrawableInflater.java:130)
at android.graphics.drawable.Drawable.createFromXmlInner (Drawable.java:1224)
at android.graphics.drawable.Drawable.createFromXml (Drawable.java:1197)
at android.content.res.ResourcesImpl.loadDrawableForCookie (ResourcesImpl.java:854)
at android.content.res.ResourcesImpl.loadDrawable (ResourcesImpl.java:710)
at android.content.res.Resources.loadDrawable (Resources.java:863)
at android.content.res.TypedArray.getDrawable (TypedArray.java:928)
at android.view.View.<init> (View.java:4189)
at android.widget.TextView.<init> (TextView.java:748)
at android.widget.Button.<init> (Button.java:109)
at android.widget.Button.<init> (Button.java:105)
at android.support.v7.widget.AppCompatButton.<init> (AppCompatButton.java:71)
at android.support.v7.widget.AppCompatButton.<init> (AppCompatButton.java:67)
at android.support.v7.app.AppCompatViewInflater.createView (AppCompatViewInflater.java:109)
at android.support.v7.app.AppCompatDelegateImplV9.createView (AppCompatDelegateImplV9.java:1024)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView (AppCompatDelegateImplV9.java:1081)
at android.view.LayoutInflater$FactoryMerger.onCreateView (LayoutInflater.java:192)
at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate (LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren (LayoutInflater.java:821)
at android.view.LayoutInflater.inflate (LayoutInflater.java:518)
at android.view.LayoutInflater.inflate (LayoutInflater.java:426)
at com.sky.breaker.pressit.tabs.Tab1.onCreateView (Tab1.java:58)
at android.support.v4.app.Fragment.performCreateView (Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState (FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps (BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps (FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether (FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute (FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execSingleAction (FragmentManager.java:2199)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss (BackStackRecord.java:651)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate (FragmentPagerAdapter.java:145)
at android.support.v4.view.ViewPager.populate (ViewPager.java:1236)
at android.support.v4.view.ViewPager.populate (ViewPager.java:1084)
at android.support.v4.view.ViewPager.onMeasure (ViewPager.java:1614)
at android.view.View.measure (View.java:19835)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6164)
at android.widget.LinearLayout.measureChildBeforeLayout (LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical (LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure (LinearLayout.java:640)
at android.view.View.measure (View.java:19835)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6164)
at android.widget.FrameLayout.onMeasure (FrameLayout.java:185)
at android.view.View.measure (View.java:19835)
at android.support.v4.widget.DrawerLayout.onMeasure (DrawerLayout.java:1060)
at android.view.View.measure (View.java:19835)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6164)
at android.widget.LinearLayout.measureChildBeforeLayout (LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical (LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure (LinearLayout.java:640)
at android.view.View.measure (View.java:19835)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6164)
at android.widget.FrameLayout.onMeasure (FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure (ContentFrameLayout.java:139)
at android.view.View.measure (View.java:19835)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6164)
at android.widget.LinearLayout.measureChildBeforeLayout (LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical (LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure (LinearLayout.java:640)
at android.view.View.measure (View.java:19835)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6164)
at android.widget.FrameLayout.onMeasure (FrameLayout.java:185)
at android.view.View.measure (View.java:19835)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6164)
at android.widget.LinearLayout.measureChildBeforeLayout (LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical (LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure (LinearLayout.java:640)
at android.view.View.measure (View.java:19835)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6164)
at android.widget.FrameLayout.onMeasure (FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure (DecorView.java:692)
at android.view.View.measure (View.java:19835)
at android.view.ViewRootImpl.performMeasure (ViewRootImpl.java:2351)
at android.view.ViewRootImpl.measureHierarchy (ViewRootImpl.java:1423)
at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:1672)
at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1299)
at android.view.ViewRootImpl$TraversalRunnable.run (ViewRootImpl.java:6558)
at android.view.Choreographer$CallbackRecord.run (Choreographer.java:871)
at android.view.Choreographer.doCallbacks (Choreographer.java:683)
at android.view.Choreographer.doFrame (Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:857)
at android.os.Handler.handleCallback (Handler.java:751)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6317)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:762)
我的片段Java类:
public class Tab1 extends Fragment {
ImageView button;
File directory;
View layout;
public static MediaPlayer mp;
public boolean sound1 = true;
View view;
public boolean soundempty;
AudioManager audioManager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab1, container, false);
view = (View)v.findViewById(R.id.activity_main);
button = (ImageView)v.findViewById(R.id.button);
button();
File storage = Environment.getExternalStorageDirectory();
directory = new File(storage.getAbsolutePath() +"/soundtest/");
return v;
}
public void button(){
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
cleanUpMediaPlayer();
mp = MediaPlayer.create(getActivity(), R.raw.sound);
mp.start();
if(!sound1 || !soundempty){
mp.setVolume(0.0f, 0.0f);
}else{
mp.setVolume(1f, 1f);
}
button.setClickable(false);
button.setImageResource(R.drawable.button1);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
button.setImageResource(R.drawable.button);
button.setClickable(true);
}
});
}catch (Exception e){
e.printStackTrace();
}
}
});
}
public static void cleanUpMediaPlayer() {
if (mp != null) {
try {
mp.stop();
mp.release();
mp = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
图像只有512x512,声音也不是那么大,我已经尝试禁用Button 5秒钟,所以没有人可以发送垃圾邮件。我还要在每次单击之前清理MediaPlayer。 我对OutOfMemory错误一无所知,所以我无法自行判断导致崩溃的原因。
感谢您的帮助。
答案 0 :(得分:0)
嗨(OOM)内存不足的发生可能有多种原因,例如:加载大型位图或保存应该被垃圾回收的大型对象的引用。现在在您的代码中很难确定,所以我有一些建议可以用来查明错误
1:使用LeakCanary库,它有助于检测和修复内存泄漏,而内存泄漏通常是OOM异常的原因
2:您正在加载2个资源,为什么不注释加载图像的代码,并查看OOM是否以这种方式发生,至少您会弄清楚图像或音频是否使您的应用崩溃。