Android 商店按钮安卓
我尝试将一些按钮存储到如下数组中:Android 商店按钮安卓,android,arrays,button,Android,Arrays,Button,我尝试将一些按钮存储到如下数组中: 按钮介绍,产品 Button[]toogleButtons={Intro,Product}; private int[] ToogleButtonID = { R.id.tab2_info_intro,R.id.tab2_info_product }; 在此之后,我初始化按钮: private void iniToogleButton() { for (int i = 0; i < toogleButtons.length; i++) {
按钮介绍,产品代码>
Button[]toogleButtons={Intro,Product};
private int[] ToogleButtonID = { R.id.tab2_info_intro,R.id.tab2_info_product };
在此之后,我初始化按钮:
private void iniToogleButton() {
for (int i = 0; i < toogleButtons.length; i++) {
toogleButtons[i] = (Button) findViewById(ToogleButtonID[i]);
toogleButtons[i].setOnClickListener(new View.OnClickListener() {}}
Intro.setBackgroundColor(Color.RED);
}
private void iniToogleButton(){
对于(int i=0;i
它在Intro.setBackgroundColor()上获得NullPointerException;
看起来我无法存储这些按钮并用数组初始化
有什么想法或好方法使它成为可能吗?使用
toogleButtons[i].setBackgroundColor(Color.RED);
而不是
Intro.setBackgroundColor(Color.RED);
要更改按钮背景色,因为在调用setBackgroundColor
之前未初始化Intro
按钮实例,需要在调用setBackgroundColor(color.RED)之前初始化Intro。
将toogleButtons重命名为这样的按钮
buttons[i] = (Button) findViewById(ToogleButtonID[i]);
buttons[i].setOnClickListener(new View.OnClickListener() {}}
Intro.setBackgroundColor(Color.RED);