Android Studio 2.0 最简单的纯图片程序闪退 为什么?

消逝伊谧
  • 19

升级Android Studio 新建项目,默认Hello World 运行没有问题,删掉TextView,换成ImageView,只加载一个图,编译运行,程序闪退,下变为源码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.happybirthday.MainActivity">

    <ImageView
        android:src="@drawable/androidplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

图片为1.19MB,2880*1800分辨率,放在drawable目录下,图片没有问题,在另一个同学的那里相同代码可以编译并顺利运行,相同代码和图片放在eclipse没有问题。
闪退后错误信息

04-10 22:46:38.733 2393-2393/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 22:46:38.733 2393-2393/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 22:46:38.733 2393-2393/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 22:46:38.733 2393-2393/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 22:46:38.833 2393-2393/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 22:46:38.833 2393-2393/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 22:46:38.833 2393-2393/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 22:46:38.833 2393-2393/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 22:46:38.833 2393-2393/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 22:46:38.833 2393-2393/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 22:46:38.833 2393-2393/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 22:46:39.283 2393-2393/com.example.android.happybirthday E/dalvikvm-heap: Out of memory on a 46656012-byte allocation.
04-10 22:46:39.303 2393-2393/com.example.android.happybirthday E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.android.happybirthday, PID: 2393
                                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.happybirthday/com.example.android.happybirthday.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class android.widget.ImageView
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:136)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                                     at dalvik.system.NativeStart.main(Native Method)
                                                                                  Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class android.widget.ImageView
                                                                                     at android.view.LayoutInflater.createView(LayoutInflater.java:621)
                                                                                     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
                                                                                     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:670)
                                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
                                                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                                                     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
                                                                                     at android.app.Activity.setContentView(Activity.java:1929)
                                                                                     at com.example.android.happybirthday.MainActivity.onCreate(MainActivity.java:11)
                                                                                     at android.app.Activity.performCreate(Activity.java:5231)
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:136) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                     at dalvik.system.NativeStart.main(Native Method) 
                                                                                  Caused by: java.lang.reflect.InvocationTargetException
                                                                                     at java.lang.reflect.Constructor.constructNative(Native Method)
                                                                                     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
                                                                                     at android.view.LayoutInflater.createView(LayoutInflater.java:595)
                                                                                     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
                                                                                     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:670) 
                                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695) 
                                                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:756) 
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                                     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 
                                                                                     at android.app.Activity.setContentView(Activity.java:1929) 
                                                                                     at com.example.android.happybirthday.MainActivity.onCreate(MainActivity.java:11) 
                                                                                     at android.app.Activity.performCreate(Activity.java:5231) 
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:136) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                     at dalvik.system.NativeStart.main(Native Method) 
                                                                                  Caused by: java.lang.OutOfMemoryError
                                                                                     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                                     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
                                                                                     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
                                                                                     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
                                                                                     at android.content.res.Resources.loadDrawable(Resources.java:2115)
                                                                                     at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                                                                                     at android.widget.ImageView.<init>(ImageView.java:129)
                                                                                     at android.widget.ImageView.<init>(ImageView.java:119)
                                                                                     at java.lang.reflect.Constructor.constructNative(Native Method) 
                                                                                     at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
                                                                                     at android.view.LayoutInflater.createView(LayoutInflater.java:595) 
                                                                                     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
                                                                                     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:670) 
                                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695) 
                                                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:756) 
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                                     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 
                                                                                     at android.app.Activity.setContentView(Activity.java:1929) 
                                                                                     at com.example.android.happybirthday.MainActivity.onCreate(MainActivity.java:11) 
                                                                                     at android.app.Activity.performCreate(Activity.java:5231) 
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:136) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                     at dalvik.system.NativeStart.main(Native Method) 

换一张小图,1254*1254分辨率的就能够顺利运行,仅仅换了张图,将那张大图放在mipmap目录下,使用@mipmap就能运行。

想着是编译器出问题了,重装了整个AS,但还是不行,并且有时候错误信息是内存泄漏或者OOM

04-10 23:05:32.881 10358-10358/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 23:05:32.881 10358-10358/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 23:05:32.881 10358-10358/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 23:05:32.881 10358-10358/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 23:05:32.881 10358-10358/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 23:05:32.881 10358-10358/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 23:05:32.881 10358-10358/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 23:05:32.881 10358-10358/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 23:05:32.881 10358-10358/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 23:05:32.881 10358-10358/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 23:05:32.881 10358-10358/com.example.android.happybirthday E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.android.happybirthday.MainActivity.access$super
04-10 23:05:33.791 10358-10358/com.example.android.happybirthday E/dalvikvm-heap: Out of memory on a 118160652-byte allocation.
04-10 23:05:33.811 10358-10358/com.example.android.happybirthday E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.example.android.happybirthday, PID: 10358
                                                                                   java.lang.OutOfMemoryError
                                                                                       at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                                       at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
                                                                                       at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
                                                                                       at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
                                                                                       at android.content.res.Resources.loadDrawable(Resources.java:2115)
                                                                                       at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                                                                                       at android.widget.ImageView.<init>(ImageView.java:129)
                                                                                       at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
                                                                                       at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
                                                                                       at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102)
                                                                                       at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
                                                                                       at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1030)
                                                                                       at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
                                                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
                                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                                                       at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
                                                                                       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
                                                                                       at com.example.android.happybirthday.MainActivity.onCreate(MainActivity.java:11)
                                                                                       at android.app.Activity.performCreate(Activity.java:5231)
                                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                       at android.os.Looper.loop(Looper.java:136)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                                       at dalvik.system.NativeStart.main(Native Method)

请各路大神帮忙!感激不尽。

回复
阅读 7.4k
7 个回答
✓ 已被采纳

撸主你这个问题我之前也遇到过,纠结了一天才解决。
如楼上所说,2880*1800的argb会占20m内存。但就算一张图20m内存,也不应该每次都出现oom,因为现在的机器几乎没有16m默认堆的情况了,比如我现在这个mxpro,前年的机器,默认256,largeheap是512。
如果情况特殊,可以在manifest文件中申请largeheap,但这不是本问题的所在。
问题出在drawable文件夹上,这个文件夹是对dpi敏感的。
如果你屏幕分辨率是1920*1080,那么,屏幕dpi是480,drawable文件夹下的图片,据我推测会当做120dpi的处理,也就是说,会将图片放大四倍,1w+的尺寸,爆内存是肯定的了。
图片描述
上图是一个project的res文件夹,对于drawable也可以有相应的dpi级别,你这张图,最好的选择是放在xxxhdpi下,这样如果在低dpi机器上时会自动缩放,还是你这个1920*1080,480dpi的机器上,实际图片大小会缩放为480/640,也就是四分之三,很大程度上优化了内存使用。
最不济,你得放在nodpi文件夹下,这样起码它不会自动放大。
最后上个Google官方的dpi对应数据
A set of six generalized densities:
ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi


http://developer.android.com/intl/zh-cn/...
Google对于这方面的官方指导,顺便补充下,我上面对于默认dpi的猜测是错误的,默认是160dpi。

我想你应该恶补一下图片在内存中存储的方式,简单的说吧,不是编译的问题,是内存爆了所以崩溃了。
给你计算你一下你这张图片所占用的内存:
2880 * 1800 = 5148000 (px)
5148000 * 4Byte = 20736000 (Byte) = 19.78 (MB)
一张图片占用近20MB的内存空间,而Android标配给一个App的内存空间只有16MB,能不爆吗?

valjeta
  • 2
新手上路,请多包涵

我看错了 不是2.0问题

你看看你的activity里有没有下面这个函数,有的话删了。

@Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { 
    super.onSaveInstanceState(outState, outPersistentState); 
}

不是吧,就是内存溢出了吧。之所以其他可以和手机有关吧,低内存自然就爆了,和AS无关,我这边也今天升级的,跑原来的应用没有问题

楼上正解,图片大小要控制啊。一个像素可是4个字节的哦

不是 AS 的问题,是图片太大,内存溢出的问题,就行楼上几位所说的。

关于显示大图片,可以参考官方的文档:Loading Large Bitmaps Efficiently

宣传栏