[英]change the android layout background randomly
I coded an activity that when user press only a button, the background would change randomly. 我编写了一个活动,当用户仅按一个按钮时,背景就会随机变化。 I put background images in drawable file,and created a array of these images in value. 我将背景图像放置在可绘制文件中,并创建了这些图像的值数组。 Here is what i have got now: 这是我现在得到的:
final LinearLayout background = (LinearLayout)findViewById(R.id.back);
Button button = (Button)findViewById(R.id.button1);
Resources res = getResources();
final TypedArray myImages = res.obtainTypedArray(R.array.image);
button.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Random r = new Random(myImages.length());
int b = r.nextInt();
background.setBackgroundResource(b);
}
});
and the array: 和数组:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="image">
<item>@drawable/background</item>
<item>@drawable/background_blue</item>
<item>@drawable/background_pink</item>
<item>@drawable/background_orange</item>
</array>
</resources>
It seems nothing wrong, but every time I start the emulator to check out, it always show that the activity stopped 似乎没有错,但是每次我启动模拟器进行检出时,它总是表明活动已停止
can any one tell me what is the problem of my code?? 谁能告诉我代码的问题是什么? I will appreciate that!!! 我将不胜感激!!!
TO:Ashok logcat show these error TO:Ashok logcat显示这些错误
E/AndroidRuntime(802): FATAL EXCEPTION: main E/AndroidRuntime(802):java.lang.ArrayIndexOutOfBoundsException:length=456;index=1647793160
E/AndroidRuntime(802): at android.content.res.TypedArray.getResourceId(TypedArray.java:570)
E/AndroidRuntime(802): at com.example.cathy.MainActivity$1.onClick(MainActivity.java:39)
E/AndroidRuntime(802): at android.view.View.performClick(View.java:4240)
E/AndroidRuntime(802): at android.view.View$PerformClick.run(View.java:17721)
E/AndroidRuntime(802): at android.os.Handler.handleCallback(Handler.java:730)
E/AndroidRuntime(802): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(802): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(802): atandroid.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime(802): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(802): at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime(802): atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(802): at dalvik.system.NativeStart.main(Native Method)
Try this 尝试这个
final LinearLayout background = (LinearLayout) findViewById(R.id.back);
Button button = (Button) findViewById(R.id.button1);
Resources res = getResources();
final TypedArray myImages = res.obtainTypedArray(R.array.image);
final Random random = new Random();
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//Genrate a random index in the range
int randomInt = random.nextInt(myImages.length());
// Generate the drawableID from the randomInt
int drawableID = myImages.getResourceId(randomInt, -1);
background.setBackgroundResource(drawableID);
}
});
The problem is how you are using setBackgroundResource()
. 问题是您如何使用setBackgroundResource()
。 It expects the integer value of the resource file, not the array index. 它期望资源文件的整数值,而不是数组索引。
Take a look here for how to get the value out of a resource array: Getting String Array From Resources 在这里看看如何从资源数组中获取值: 从资源中获取字符串数组
edit: Apologies, missed the beginning of your code. 编辑:抱歉,错过了代码的开头。 Try something like this: 尝试这样的事情:
background.setBackgroundResource(myImages.getResourceId(b,-1));
You might want to add error checking to see what getResourceId()
returns first though. 您可能想添加错误检查,以查看getResourceId()
首先返回的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.