gpt4 book ai didi

android - 使 ImageView 从方法中不可见

转载 作者:行者123 更新时间:2023-11-29 15:26:14 24 4
gpt4 key购买 nike

我创建了如下所示的 HideImages() 函数。问题是,运行此代码会导致 NullPointerExcpection。当我注释掉 setVisibility 行时,它工作正常。我做错了什么?

public class MainActivity extends Activity implements SurfaceHolder.Callback {
ImageView img_w0, img_w1, img_w2;
public void onCreate(Bundle savedInstanceState) {
ImageView img_w0 = (ImageView)findViewById(R.id.img0);
ImageView img_w1 = (ImageView)findViewById(R.id.img1);
ImageView img_w2 = (ImageView)findViewById(R.id.img2);

HideImages();

}
public void HideImages() {
img_w0.setVisibility(View.INVISIBLE);
img_w1.setVisibility(View.INVISIBLE);
img_w2.setVisibility(View.INVISIBLE);
}
}

最佳答案

将ImageView的所有引用设为Global as

public class MainActivity extends Activity implements SurfaceHolder.Callback {
ImageView img_w0, img_w1, img_w2;
public void onCreate(Bundle savedInstanceState) {
img_w0 = (ImageView)findViewById(R.id.img0);
img_w1 = (ImageView)findViewById(R.id.img1);
img_w2 = (ImageView)findViewById(R.id.img2);

HideImages();

}
public void HideImages() {
img_w0.setVisibility(View.INVISIBLE);
img_w1.setVisibility(View.INVISIBLE);
img_w2.setVisibility(View.INVISIBLE);
}
}

关于android - 使 ImageView 从方法中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12724174/

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