我正在尝试实现一个应用程序,其中在创建活动时在图像视图中加载大量图像 - 在滚动视图中正好有24个图像视图对齐。 当我用24 imageview作为启动器活动本身开始该活动时 - 它工作正常.. 但是,当我在欢迎屏幕之后将该活动用作第二个活动时,它会崩溃并说 - >
12-29 11:54:04.624 31595-31595/com.example.myvidapp W/art: Throwing OutOfMemoryError "Failed to allocate a 6822556 byte allocation with 3024640 free bytes and 2MB until OOM"
12-29 11:54:04.633 31595-31595/com.example.myvidapp D/skia: --- allocation failed for scaled bitmap
12-29 11:54:04.634 31595-31595/com.example.myvidapp D/AndroidRuntime: Shutting down VM
12-29 11:54:04.635 31595-31595/com.example.myvidapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myvidapp, PID: 31595
java.lang.OutOfMemoryError: Failed to allocate a 6822556 byte allocation with 3024640 free bytes and 2MB until OOM
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:1155)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
at android.content.res.Resources.loadDrawable(Resources.java:858)
at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
at android.view.View.<init>(View.java:4202)
at android.widget.ImageView.<init>(ImageView.java:155)
at android.widget.ImageView.<init>(ImageView.java:150)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)
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.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
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 android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.myvidapp.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:6723)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2626)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2734)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1485)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6195)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
我如何克服这个错误?
这是我的xml文件 - &gt;
<ScrollView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/bkg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Video Chat MadeEasy"
android:textColor="#191fcc"
android:textStyle="bold"
android:textSize="30sp"
android:gravity="center"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Most Popular collections!!"
android:textSize="20sp"
android:gravity="center"
android:layout_marginBottom="10dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_marginBottom="20dp"
>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/skype"
android:tooltipText="Skype"
android:layout_margin="5dp"
android:onClick="skype_clk" >
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/whapp"
android:tooltipText="WhatsApp"
android:layout_margin="5dp"
android:onClick="whp_clk">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/msng"
android:layout_margin="5dp"
android:onClick="msg_clk"
android:tooltipText="Messenger">
</ImageView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_marginBottom="20dp">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/imo"
android:tooltipText="imo"
android:layout_margin="5dp"
android:onClick="imo_clk">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/line"
android:tooltipText="Line"
android:layout_margin="5dp"
android:onClick="lin_clk">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/tango"
android:layout_margin="5dp"
android:onClick="tng_clk"
android:tooltipText="Tango">
</ImageView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_marginBottom="20dp" >
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/viber"
android:layout_margin="5dp"
android:onClick="vbr_clk"
android:tooltipText="Viber">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/icq"
android:layout_margin="5dp"
android:onClick="icq_clk"
android:tooltipText="icq">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/wechat"
android:tooltipText="weChat"
android:layout_margin="5dp"
android:onClick="wct_clk">
</ImageView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_marginBottom="20dp" >
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/ghangout"
android:tooltipText="Hangout"
android:layout_margin="5dp"
android:onClick="ght_clk">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/duo"
android:onClick="duo_clk"
android:tooltipText="Duo"></ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/bigo"
android:tooltipText="Bigo Live"
android:layout_margin="5dp"
android:onClick="bgo_clk">
</ImageView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="More from us!!"
android:textColor="#191fcc"
android:textStyle="bold"
android:textSize="30sp"
android:gravity="center"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Untraditional collections!!"
android:textSize="20sp"
android:gravity="center"
android:layout_marginBottom="10dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_marginBottom="20dp"
>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/snct"
android:tooltipText="Snapchat"
android:layout_margin="5dp"
android:onClick="snct_clk" >
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/livu"
android:tooltipText="LivU"
android:layout_margin="5dp"
android:onClick="livu_clk">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/kct"
android:layout_margin="5dp"
android:onClick="kct_clk"
android:tooltipText="KingsChat">
</ImageView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_marginBottom="20dp">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/lly"
android:tooltipText="live.ly"
android:layout_margin="5dp"
android:onClick="lly_clk">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/mpl"
android:tooltipText="Marcopolo"
android:layout_margin="5dp"
android:onClick="mpl_clk">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/now"
android:layout_margin="5dp"
android:onClick="now_clk"
android:tooltipText="YouNow">
</ImageView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_marginBottom="20dp" >
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/lme"
android:layout_margin="5dp"
android:onClick="lme_clk"
android:tooltipText="live.me">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/sgo"
android:layout_margin="5dp"
android:onClick="sgo_clk"
android:tooltipText="Streamgo">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/hny"
android:tooltipText="Honey"
android:layout_margin="5dp"
android:onClick="hny_clk">
</ImageView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_marginBottom="20dp" >
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/mln"
android:tooltipText="Melon"
android:layout_margin="5dp"
android:onClick="mln_clk">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/imn"
android:onClick="imn_clk"
android:tooltipText="Indian Messsenger"></ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/hk"
android:tooltipText="Hike"
android:layout_margin="5dp"
android:onClick="hk_clk">
</ImageView>
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Want to explore more ? Click here !!"
android:onClick="btnpl"
android:background="@drawable/buttonshape"
android:textColor="#ffffff"/>
</LinearLayout>
</ScrollView>
这里是第15行,它在&#34; onCreate&#34; - &gt;
上显示错误-i.e @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //line no 15 in my code(indicated in the logcat)
}
答案 0 :(得分:1)
添加清单文件android:hardwareAccelerated =&#34; false&#34; ,android:largeHeap =&#34; true&#34;它适用于某些环境。
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
答案 1 :(得分:1)
在你的清单中使用它:
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
答案 2 :(得分:0)
使用Picasso
和Glide
等图片加载库。对于public static void loadToGlide(Context context, ImageView imageView, Object o) {
Glide.with(context)
.asBitmap()
.apply(new RequestOptions()
.centerCrop())
.transition(new BitmapTransitionOptions()
.crossFade())
.load(o)
.into(imageView);
}
:
loadToGlide(context, yourImageView, R.drawable.yourImage);
致电:
OutOfMemoryError
他们将为您处理一切,尤其是{{1}}。
答案 3 :(得分:0)
您可以使用recylerview而不是多个imagviews,并且可以使用volley imageloader或glide来加载seamlessely。