【问题标题】:Store Button Android商店按钮 Android
【发布时间】:2013-12-05 02:17:51
【问题描述】:

我尝试将一些按钮存储到这样的数组中:

Button Intro,Product;

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);
}

它在 Intro.setBackgroundColor() 上得到 NullPointerException; 看起来我无法存储这些按钮并使用数组进行初始化。

有什么想法或好方法可以使它成为可能吗?

【问题讨论】:

    标签: android arrays button


    【解决方案1】:

    使用

    toogleButtons[i].setBackgroundColor(Color.RED);
    

    而不是

    Intro.setBackgroundColor(Color.RED);
    

    更改按钮背景颜色,因为您在调用setBackgroundColor 之前没有初始化Intro Button 实例

    【讨论】:

      【解决方案2】:

      您需要在调用 setBackgroundColor(Color.RED) 之前初始化 Intro。 将工具按钮重命名为这样的按钮,

      buttons[i] = (Button) findViewById(ToogleButtonID[i]);
      buttons[i].setOnClickListener(new View.OnClickListener() {}}
      Intro.setBackgroundColor(Color.RED);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-17
        • 1970-01-01
        • 2019-04-12
        • 1970-01-01
        • 2012-06-21
        • 2018-12-17
        • 2018-02-01
        • 2016-01-01
        相关资源
        最近更新 更多