Android 为什么setVisibility()在for语句中工作?
关于这个话题有很多问题,但没有一个能解决我的问题 我有一个Android 为什么setVisibility()在for语句中工作?,android,for-loop,android-imageview,Android,For Loop,Android Imageview,关于这个话题有很多问题,但没有一个能解决我的问题 我有一个ImageView数组,通过for语句,我想让它们不可见。代码如下 final ImageView[] image = new ImageView[12]; image[0] = (ImageView) findViewById(R.id.imageView1); image[1] = (ImageView) findViewById(R.id.imageView2); image[2] = (ImageView) findVie
ImageView
数组,通过for语句,我想让它们不可见。代码如下
final ImageView[] image = new ImageView[12];
image[0] = (ImageView) findViewById(R.id.imageView1);
image[1] = (ImageView) findViewById(R.id.imageView2);
image[2] = (ImageView) findViewById(R.id.imageView3);
image[3] = (ImageView) findViewById(R.id.imageView4);
for (int p = 0; p < 4; p++) {
image[p].setVisibility(View.INVISIBLE);
}
final ImageView[]image=new ImageView[12];
图像[0]=(ImageView)findViewById(R.id.imageView1);
图像[1]=(ImageView)findViewById(R.id.imageView2);
图像[2]=(ImageView)findViewById(R.id.imageView3);
图像[3]=(ImageView)findViewById(R.id.imageView4);
对于(int p=0;p<4;p++){
图像[p].setVisibility(视图.不可见);
}
问题似乎是把p
作为image[]
的参数,我认为是这样的,因为如果我把p
放在一个数字上,它就会起作用。试着用它来使用Varargs
public void hideViews(View... views)
{ //it will work with parent class but you can change it to ImageView
for (View view : views) {
view.setVisibility(View.INVISIBLE);
}
}
电话:
hideViews(image1,image2,image3,image4);
祝你好运 尝试使用它使用的Varargs
public void hideViews(View... views)
{ //it will work with parent class but you can change it to ImageView
for (View view : views) {
view.setVisibility(View.INVISIBLE);
}
}
电话:
hideViews(image1,image2,image3,image4);
祝你好运 p
是一个数字。应该编译。这是运行时问题吗?如果是,请将堆栈跟踪添加到问题中。虽然在这种情况下,它可能是一个NullPointerException
引起的,因为给findViewById()
@AhamadAnees的id错误,我不知道,如果我写的是image[2]
它工作,如果我写的是image[p]
它不工作p
是一个数字。这应该编译。这是运行时问题吗?如果是,请将堆栈跟踪添加到问题中。虽然在这种情况下,它可能是一个NullPointerException
引起的,因为给findViewById()
@AhamadAnees的id不正确,我不知道,如果我写了例如image[2]
它工作,如果我写image[p]
它不工作,谢谢,但是视图之间写了什么。。。查看
?@user7867367其名为Varargs阅读更多信息,如果您觉得问题有用,请向上投票谢谢,但是查看之间写了什么。。。视图
?@user7867367其名为Varargs阅读更多信息,如果您觉得有帮助,请向上投票