我有几张图片和两个按钮。我正在使用Next和Back两个按钮更改我的图像视图。我的下一个按钮工作正常,它改变了前向排列中的图像,而我希望我的后退按钮以相反的方向进行排列。它工作了一段时间,但当它到达阵列中的第一个图像时,应用程序崩溃了。这是我的代码:
private int[] drawables = { R.drawable.bg, R.drawable.green,
R.drawable.blue };
private int clicks = 0;
private int clicks1 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.learn_main);
Button btnNext = (Button) findViewById(R.id.button2);
Button btnBack = (Button) findViewById(R.id.button1);
final ImageView img = (ImageView) findViewById(R.id.imageView1);
// INITIALIZING OVER
btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (clicks < drawables.length - 1) {
clicks++;
} else {
clicks = 0;
}
img.setImageResource(drawables[clicks]);
}
});
btnBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (clicks1 < drawables.length + 1) {
clicks1++;
} else {
clicks1 = 0;
}
img.setImageResource(drawables[clicks1]);
}
});
}
}
logcat的
04-28 09:56:31.782: E/AndroidRuntime(2563): FATAL EXCEPTION: main
04-28 09:56:31.782: E/AndroidRuntime(2563): java.lang.ArrayIndexOutOfBoundsException
04-28 09:56:31.782: E/AndroidRuntime(2563): at com.example.duh.Learn$2.onClick(Learn.java:60)
04-28 09:56:31.782: E/AndroidRuntime(2563): at android.view.View.performClick(View.java:2533)
04-28 09:56:31.782: E/AndroidRuntime(2563): at android.view.View$PerformClick.run(View.java:9320)
04-28 09:56:31.782: E/AndroidRuntime(2563): at android.os.Handler.handleCallback(Handler.java:587)
04-28 09:56:31.782: E/AndroidRuntime(2563): at android.os.Handler.dispatchMessage(Handler.java:92)
04-28 09:56:31.782: E/AndroidRuntime(2563): at android.os.Looper.loop(Looper.java:150)
04-28 09:56:31.782: E/AndroidRuntime(2563): at android.app.ActivityThread.main(ActivityThread.java:4389)
04-28 09:56:31.782: E/AndroidRuntime(2563): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 09:56:31.782: E/AndroidRuntime(2563): at java.lang.reflect.Method.invoke(Method.java:507)
04-28 09:56:31.782: E/AndroidRuntime(2563): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
04-28 09:56:31.782: E/AndroidRuntime(2563): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-28 09:56:31.782: E/AndroidRuntime(2563): at dalvik.system.NativeStart.main(Native Method)
我尽力详细解释我的问题,如果我没有正确解释,请告诉我,我会编辑我的问题。
答案 0 :(得分:0)
尝试使用单变量int clicks = 0
btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (clicks < drawables.length-1) {
img.setImageResource(drawables[clicks++]);
}
}
});
btnBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
clicks--;
if (clicks>=0) {
img.setImageResource(drawables[clicks]);
}
}
});
答案 1 :(得分:0)
我其实想发表评论,但我的代表是41,所以,看看后面的按钮功能你给出了点击+1 ++,但是drawables.length = 3和+1你这样做它的4 ..所以当click1 = 2时它会进入并且点击1 ++并且当它试图设置drawable(clicks1)[可绘制[3]]时它将超出范围coz drawable [2]是可用的最后一个元素...改变你的逻辑..
答案 2 :(得分:0)
这是解决您问题的方法 declare variable int index = 0;然后在oncreate下面编写代码,你就完成了
img.setImageResource(drawables[index]);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (index < drawables.length-1) {
index++;
} else {
index = 0;
}
img.setImageResource(drawables[index]);
}
});
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (index > 0) {
index--;
} else {
index = drawables.length-1;
}
img.setImageResource(drawables[index]);
}
});
答案 3 :(得分:0)
使用以下测试的代码
int clicks = -1
btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (clicks < drawables.length - 1) {
clicks++;
img.setImageResource(drawables[clicks]);
}
}
});
btnBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (clicks > 0) {
clicks--;
img.setImageResource(drawables[clicks]);
}
}
});