问题描述
我的Android应用程序出错了。
我想使用viewPager作为android图像滑块,为此我得到了我的AndroidImageAdapter.java,但是当涉及方法instantiateItem时,应用程序中止,我不知道如何解决它。
我已查找失败并添加
I got an error with my android application.I want to use a viewPager as an android image slider, for this I got my AndroidImageAdapter.java, but when it comes to the method instantiateItem the application aborts and I don't know how to fix it.I already looked up the failure and added
android:hardwareAccelerated="false"
android:largeHeap="true"
到我的清单,但它仍然存在。
以下是完整的错误代码:
to my manifest, but it still remains.Here is the complete error code:
java.lang.OutOfMemoryError: Failed to allocate a 571401228 byte allocation with 16777216 free bytes and 495MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:4211)
at android.content.res.Resources.loadDrawable(Resources.java:4085)
at android.content.res.Resources.getDrawable(Resources.java:2005)
at android.content.res.Resources.getDrawable(Resources.java:1987)
at android.content.Context.getDrawable(Context.java:464)
at android.widget.ImageView.resolveUri(ImageView.java:827)
at android.widget.ImageView.setImageResource(ImageView.java:434)
at bubusapp.anni.privateapplication.com.schnurr.asam.robin.bubusapp.AndroidImageAdapter.instantiateItem(AndroidImageAdapter.java:50)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1150)
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:20151)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
at android.view.View.measure(View.java:20151)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1549)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
at android.view.Choreographer.doCallbacks(Choreographer.java:695)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(Zy
我的java代码:
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class AndroidImageAdapter extends PagerAdapter{
private Context mContext;
private int[] images;
AndroidImageAdapter(Context context, int remainingDays) {
this.mContext = context;
images = loadImages(remainingDays);
}
@Override
public int getCount() {
return images.length;
}
@Override
public boolean isViewFromObject(View v, Object obj) {
return v == ((ImageView) obj);
}
@Override
public Object instantiateItem(ViewGroup container, int i) {
ImageView mImageView = new ImageView(mContext);
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
try
{
mImageView.setImageResource(images[i]);//here comes the exception - outofmemory
}
catch(Exception ex)
{
Log.i("imageResException", ex.toString());
}
((ViewPager) container).addView(mImageView, 0);
return mImageView;
}
@Override
public void destroyItem(ViewGroup container, int i, Object obj) {
((ViewPager) container).removeView((ImageView) obj);
}
private int[] loadImages(int remainingDays)
{
int[] allImages = new int[] {R.drawable.pos1_1, R.drawable.pos1_2, R.drawable.pos1_3, R.drawable.pos1_4, R.drawable.pos1_5, R.drawable.pos1_6, R.drawable.pos1_7, R.drawable.pos1_8, R.drawable.pos1_9, R.drawable.pos1_10, R.drawable.pos1_11,
R.drawable.pos1_12, R.drawable.pos1_13, R.drawable.pos1_14, R.drawable.pos1_15, R.drawable.pos1_16, R.drawable.pos1_17, R.drawable.pos1_18, R.drawable.pos1_19,R.drawable.pos1_20, R.drawable.pos1_21, R.drawable.pos1_22, R.drawable.pos1_23, R.drawable.pos1_24, R.drawable.pos1_25, R.drawable.pos1_26, R.drawable.pos1_27,
R.drawable.pos1_28,
R.drawable.pos1_29,
R.drawable.pos1_30,
R.drawable.pos1_31,
R.drawable.pos1_32,
R.drawable.pos1_33,
R.drawable.pos1_34,
R.drawable.pos1_35,
R.drawable.pos1_36,
R.drawable.pos1_37,
R.drawable.pos1_38,
R.drawable.pos1_39,
R.drawable.pos1_40,
R.drawable.pos1_41,
R.drawable.pos1_42,
R.drawable.pos1_43,
R.drawable.pos1_44,
R.drawable.pos1_45,
R.drawable.pos1_46,
R.drawable.pos1_47,
R.drawable.pos1_48,
R.drawable.pos1_49,
R.drawable.pos1_50,
R.drawable.pos1_51,
R.drawable.pos1_52,
R.drawable.pos1_53,
R.drawable.pos1_54,
R.drawable.pos1_55,
R.drawable.pos1_56,
R.drawable.pos1_57,
R.drawable.pos1_58,
R.drawable.pos1_59,
R.drawable.pos1_60,
R.drawable.pos1_61,
R.drawable.pos1_62,
R.drawable.pos1_63,
R.drawable.pos1_64,
R.drawable.pos1_65,
R.drawable.pos1_66,
R.drawable.pos1_67,
R.drawable.pos1_68,
R.drawable.pos1_69,
R.drawable.pos1_70,
R.drawable.pos1_71,
R.drawable.pos1_72,
R.drawable.pos1_73,
R.drawable.pos1_74,
R.drawable.pos1_75,
R.drawable.pos1_76,
R.drawable.pos1_77,
R.drawable.pos1_78,
R.drawable.pos1_79,
R.drawable.pos1_80,
R.drawable.pos1_81,
R.drawable.pos1_82,
R.drawable.pos1_83,
R.drawable.pos1_84,
R.drawable.pos1_85,
R.drawable.pos1_86,
R.drawable.pos1_87,
R.drawable.pos1_88,
R.drawable.pos1_89,
R.drawable.pos1_90,
R.drawable.pos1_91,
R.drawable.pos1_92,
R.drawable.pos1_93,
R.drawable.pos1_94,
R.drawable.pos1_95,
R.drawable.pos1_96,
R.drawable.pos1_97,
R.drawable.pos1_98,
R.drawable.pos1_99,
R.drawable.pos1_100,
R.drawable.pos1_101,
R.drawable.pos1_102,
R.drawable.pos1_103,
R.drawable.pos1_104,
R.drawable.pos1_105,
R.drawable.pos1_106,
R.drawable.pos1_107,
R.drawable.pos1_108,
R.drawable.pos1_109,
R.drawable.pos1_110,
R.drawable.pos1_111,
R.drawable.pos1_112,
R.drawable.pos1_113,
R.drawable.pos1_114,
R.drawable.pos1_115,
R.drawable.pos1_116,
R.drawable.pos1_117,
R.drawable.pos1_118,
R.drawable.pos1_119,
R.drawable.pos1_120,
R.drawable.pos1_121,
R.drawable.pos1_122,
R.drawable.pos1_123,
R.drawable.pos1_124,
R.drawable.pos1_125,
R.drawable.pos1_126
};
int[] actualImages = new int[127 - remainingDays];
try
{
System.arraycopy(allImages, 0, actualImages, 0, 127 - remainingDays);
}
catch(Exception ex)
{
Log.i("ArrayCopyException: ", ex.getMessage() + "" );
}
return actualImages;
}
}
推荐答案
571401228字节相当于11952 x 11952像素图像。这太大了。
571401228 bytes is the equivalent of 11952 x 11952 pixel image. This is much too large.
最有可能的问题是双重问题:
Most likely, the problem is two-fold:
-
可绘制资源的分辨率本身太大
The drawable resource's resolution is too large in its own right
您将图像放入 res / drawable /
,因此它会根据您的实际设备密度进行升级,因此它比您开始时大4倍或8倍
You put the image in res/drawable/
, and so it is being upscaled based on your actual device density, so it is 4x or 8x larger than what you started with
不要将位图放在 res / drawable /
中,这是 res / drawable-mdpi /的同义词
。要么为各种密度创建与drawables密度相关的版本,要么将此drawable放在 res / drawable-anydpi /
中。另外,请确保分辨率合理。
Do not put bitmaps in res/drawable/
, which is a synonym for res/drawable-mdpi/
. Either create density-dependent editions of the drawables for various densities, or put this drawable in res/drawable-anydpi/
. Also, make sure that the resolution is reasonable.
这篇关于java.lang.OutOfMemoryError:无法分配带有16777216个空闲字节的571401228字节分配和直到OOM的495MB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!