gpt4 book ai didi

java - 将随机图像设置为imageview android

转载 作者:太空宇宙 更新时间:2023-11-04 13:40:03 24 4
gpt4 key购买 nike

我想制作一个位图数组,然后在我的 Activity 中将随机元素设置为 5 个 ImageView 。

Bitmap[] vector = new Bitmap[52];
int[] randomNumbers = new int[5];

private void createImages() {

vector [0] = BitmapFactory.decodeResource(getResources(), R.drawable.aphoto);

vector [1] = BitmapFactory.decodeResource(getResources(), R.drawable.anotherphoto);
vector [2] = BitmapFactory.decodeResource(getResources(), R.drawable.backinbusiness);
vector [3] = BitmapFactory.decodeResource(getResources(), R.drawable.heythere);
vector [4] = BitmapFactory.decodeResource(getResources(), R.drawable.whasup);

// and so on
}

private boolean randomNumbersContains(int n, int count) {
for(int i = 0; i < count; i++)
if(randomNumbers[i] == n)
return true;
return false;
}

private void getRandomNumbers() {
Random r = new Random();
int nr;
randomNumbers[0] = r.nextInt(40) + 1;

for(int i = 1; i < 5; i ++) {
do {
nr = r.nextInt(40) + 1;
} while (randomNumbersContains(nr, i));

randomNumbers[i] = nr;
}
}

@Override
public void onClick(View v) {
getRandomNumbers();

iv1.setImageBitmap(vector [randomNumbers[0]]);
iv2.setImageBitmap(vector [randomNumbers[1]]);
iv3.setImageBitmap(vector [randomNumbers[2]]);
iv4.setImageBitmap(vector [randomNumbers[3]]);
iv5.setImageBitmap(vector [randomNumbers[4]]);
}

这是我的尝试。每次单击按钮时,我都希望从阵列中再取出 5 张卡片。

我不知道为什么我的应用程序崩溃了。请帮忙!

这是日志:

07-09 11:34:50.059    8307-8307/com.blabla.bla.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.blabla.bla.app, PID: 8307
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blabla.bla.app/com.blabla.bla.app.PlayActivity}: android.view.InflateException: Binary XML file line #52: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.view.InflateException: Binary XML file line #52: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at android.app.Activity.setContentView(Activity.java:2145)
at com.symplyfyweb.gherghina.oldschoolwesternpoker.PlayActivity.onCreate(PlayActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
            at android.app.Activity.setContentView(Activity.java:2145)
            at com.blabla.bla.app.PlayActivity.onCreate(PlayActivity.java:41)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 13068012 byte allocation with 1329116 free bytes and 1297KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2474)
at android.content.res.Resources.loadDrawable(Resources.java:2381)
at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at android.widget.ImageView.<init>(ImageView.java:146)
at android.widget.ImageView.<init>(ImageView.java:135)
at android.widget.ImageView.<init>(ImageView.java:131)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
            at android.app.Activity.setContentView(Activity.java:2145)
            at com.blabla.bla.app.PlayActivity.onCreate(PlayActivity.java:41)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
07-09 11:34:57.310 8307-8318/com.blabla.bla.app I/art﹕ Clamp target GC heap from 64MB to 64MB

最佳答案

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 13068012 byte allocation with 1329116 free bytes and 1297KB until OOM

这就是导致您错误的原因。这意味着您使用了太多内存。您可以尝试在数组中保存向下渲染的版本,或者在将每个位图分配给 ImageView 之前创建每个位图。

关于java - 将随机图像设置为imageview android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31316069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com