【问题标题】:Why is my array of views returning null?为什么我的视图数组返回 null?
【发布时间】:2014-03-03 15:50:49
【问题描述】:

我在 android 中有一组开关;我们就叫他们吧……

Switch one;
Switch two;
Switch three;
Switch four;
Switch five;

我还有一个包含这些视图的数组。

Switch[] switchArray = {one, two, three, four, five};

然后,在我的 onCreateView() 方法中;我使用 findViewById() 分配所有这些 Switch。

one = (Switch) findViewById(R.id.switchOne);
two = (Switch) findViewById(R.id.switchTwo);
three = (Switch) findViewById(R.id.switchThree);
four = (Switch) findViewById(R.id.switchFour);
five = (Switch) findViewById(R.id.switchFive);

现在;当我创建一个 for 循环来检查该数组是否为空时:

    for(int i = 0; i<switchArray.length; i++){
      if(switchArray[i] == null){
         Log.d(TAG, "Array is null at:" + i);
     }
    }

我得到以下日志:

Array is null at: 1
Array is null at: 2
Array is null at: 3
Array is null at: 4
Array is null at: 5

如果我尝试在 onCreate 中实例化它们,我不确定为什么这些变量返回 null ...如果我还尝试在 onCreate 之前实例化它们(在任何方法之前的类头中);我仍然遇到同样的错误。

希望问题很清楚。

编辑 1:完整的 ONCREATEVIEW 方法

public View onCreateView(LayoutInflater viewInflation, ViewGroup container,
            Bundle SavedInstantState) {
        superContext = getActivity().getApplicationContext();
        digitalfragmentview = viewInflation.inflate(
                R.layout.digitalfragment_page, container, false);

        digitalIO0Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio0mode);
        digitalIO1Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio1mode);
        digitalIO2Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio2mode);
        digitalIO3Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio3mode);
        digitalIO4Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio4mode);
        digitalIO5Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio5mode);
        digitalIO6Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio6mode);
        digitalIO7Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio7mode);
        digitalIO8Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio8mode);
        digitalIO9Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio9mode);

        // sets the listener for the mode switches
        for (int i = 0; i < digitalIOModeSwitchArray.length; i++) {
            if (digitalIOModeSwitchArray[i] == null) {
                Log.d(TAG, "Array is null at:" + i);
            }
        }

        return digitalfragmentview;
    }

【问题讨论】:

  • 什么时候实例化 switchArray ?您需要在执行“findViewById”调用后实例化它
  • switchArray 在 onCreateView() 之前被实例化;与所有其他类变量一起位于类的顶部。
  • 在我的最新版本中; digitalIO0Mode相当于Switch One;等等等等……
  • 您的数组不会以这种方式更新。您应该在调用 findViewById 后实例化您的数组。
  • 好的,我现在会尝试并回复您。

标签: java android arrays nullpointerexception views


【解决方案1】:
Switch[] switchArray = {one, two, three, four, five};

在您创建数组时包含对one, two, three, four, five 的引用。

稍后,您重新分配这些变量,但您的数组仍然指向以前的引用。

您需要在分配视图后创建数组:

one = (Switch) findViewById(R.id.switchOne);
two = (Switch) findViewById(R.id.switchTwo);
three = (Switch) findViewById(R.id.switchThree);
four = (Switch) findViewById(R.id.switchFour);
five = (Switch) findViewById(R.id.switchFive);
// Then only
switchArray = {one, two, three, four, five};

【讨论】:

  • 有没有一种方法可以使用我的类中的方法访问该数组,然后如果它现在超出了我类中其他方法的范围?
  • 你收到评论通知了吗?
  • 你不需要改变它的范围。你可以将它声明为你的类的成员,并且仍然在 onCreate 方法中定义它。
【解决方案2】:

首先在变量中引用您的小部件 one = (Switch) findViewById(R.id.switchOne); two = (Switch) findViewById(R.id.switchTwo); three = (Switch) findViewById(R.id.switchThree); four = (Switch) findViewById(R.id.switchFour); five = (Switch) findViewById(R.id.switchFive);

然后将它们放入一个数组中

Switch[] switchArray = {one, two, three, four, five};

原因是Switch数组是值类型而不是引用类型

【讨论】:

    【解决方案3】:

    试试这个..

    one = (Switch) findViewById(R.id.switchOne);
    

    而不是这个..

    one = findViewById(R.id.switchOne);
    

    编辑

    switchArray[0] = (Switch) findViewById(R.id.switchOne);
    

    【讨论】:

    • 我很抱歉;我已经知道我错误地格式化了我的代码。请参考我的最新更新。
    • @Tukajo 你能把你的完整版onCreateView()
    • 已添加。
    • @Tukajo 是不是所有的开关都在digitalfragment_page.xml
    • 是的;它们都在 digitalfragment_page.xml 中。
    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多