Java 为什么我的视图数组返回null?

Java 为什么我的视图数组返回null?,java,android,arrays,nullpointerexception,views,Java,Android,Arrays,Nullpointerexception,Views,我有一套安卓系统的开关;让我们叫他们 Switch one; Switch two; Switch three; Switch four; Switch five; 我还有一个包含这些视图的数组 Switch[] switchArray = {one, two, three, four, five}; 然后,在我的onCreateView()方法中;我使用findviewbyd()分配所有这些开关 现在;当我创建for循环以检查该数组是否为null时: for(int i = 0;

我有一套安卓系统的开关;让我们叫他们

Switch one;
Switch two;
Switch three;
Switch four;
Switch five;
我还有一个包含这些视图的数组

Switch[] switchArray = {one, two, three, four, five};
然后,在我的onCreateView()方法中;我使用findviewbyd()分配所有这些开关

现在;当我创建for循环以检查该数组是否为null时:

    for(int i = 0; i<switchArray.length; i++){
      if(switchArray[i] == null){
         Log.d(TAG, "Array is null at:" + i);
     }
    }
如果我试图在onCreate中实例化这些变量,我不确定为什么这些变量会返回null。。。如果我还尝试在onCreate之前实例化它们(在任何方法之前的类头中);我仍然会犯同样的错误

希望问题是清楚的

编辑1:FullOnCreateView方法

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;
    }
public View onCreateView(布局平面视图、视图组容器、,
Bundle SavedInstantState){
超级上下文=getActivity().getApplicationContext();
digitalfragmentview=viewInflation.Inflation(
R.layout.u页,容器,假);
digitalIO0Mode=(开关)digitalfragmentview
.findViewById(R.id.digitalio0mode);
digitalIO1Mode=(开关)digitalfragmentview
.findviewbyd(R.id.digitalio1模式);
digitalIO2Mode=(开关)数字碎片视图
.findviewbyd(R.id.digitalio2mode);
digitalIO3Mode=(开关)digitalfragmentview
.findviewbyd(R.id.digitalio3mode);
digitalIO4Mode=(开关)digitalfragmentview
.findviewbyd(R.id.digitalio4mode);
digitalIO5Mode=(开关)digitalfragmentview
.findViewById(R.id.digitalio5mode);
digitalIO6Mode=(开关)digitalfragmentview
.findviewbyd(R.id.digitalio6mode);
digitalIO7Mode=(开关)digitalfragmentview
.findviewbyd(R.id.digitalio7mode);
digitalIO8Mode=(开关)digitalfragmentview
.findviewbyd(R.id.digitalio8mode);
digitalIO9Mode=(开关)digitalfragmentview
.findviewbyd(R.id.digitalio9mode);
//设置模式开关的侦听器
对于(int i=0;i
试试这个

one = (Switch) findViewById(R.id.switchOne);
而不是这个

one = findViewById(R.id.switchOne);
编辑

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

one = (Switch) findViewById(R.id.switchOne);
而不是这个

one = findViewById(R.id.switchOne);
编辑

switchArray[0] = (Switch) findViewById(R.id.switchOne);
包含创建数组时对
1,2,3,4,5
的引用

后一种方法是重新分配这些变量,但数组仍然指向以前的引用

指定视图后,需要创建阵列:

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};
包含创建数组时对
1,2,3,4,5
的引用

后一种方法是重新分配这些变量,但数组仍然指向以前的引用

指定视图后,需要创建阵列:

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

首先在变量中引用小部件
one=(开关)findViewById(R.id.switchOne);
two=(开关)findviewbyd(R.id.switchTwo);
三=(开关)findViewById(R.id.switchThree);
four=(开关)findviewbyd(R.id.switchFour);
five=(开关)findViewById(R.id.switchFive)

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

Switch[]switchArray={1,2,3,4,5}


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

首先在变量中引用小部件
one=(开关)findViewById(R.id.switchOne);
two=(开关)findviewbyd(R.id.switchTwo);
三=(开关)findViewById(R.id.switchThree);
four=(开关)findviewbyd(R.id.switchFour);
five=(开关)findViewById(R.id.switchFive)

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

Switch[]switchArray={1,2,3,4,5}



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

对不起;我已经知道了,我的代码格式不正确。请参阅我的最新更新。@Tukajo您可以发布您的完整
onCreateView()
添加内容吗。@Tukajo表示所有开关都位于
digitalfragment\u page.xml
是;它们都在digitalfragment_page.xml中;我已经知道了,我的代码格式不正确。请参阅我的最新更新。@Tukajo您可以发布您的完整
onCreateView()
添加内容吗。@Tukajo表示所有开关都位于
digitalfragment\u page.xml
是;它们都在digitalfragment_page.xml中。您何时实例化switchArray?您需要在执行“findViewById”调用后实例化它switchArray在onCreateView()之前实例化;在我的最新版本中;digitalIO0Mode相当于开关一;等等…您的数组将不会以这种方式更新。你应该在findViewById调用后实例化你的数组。好的,我现在就尝试一下,然后再联系你。你什么时候实例化switchArray?您需要在执行“findViewById”调用后实例化它switchArray在onCreateView()之前实例化;在我的最新版本中;digitalIO0Mode相当于开关一;等等…您的数组将不会以这种方式更新。你应该在findViewById调用后实例化你的数组。好的,我现在会尝试并返回给你。如果数组现在不在我的类中的其他方法的作用域内,有没有办法用我的类中的方法访问该数组?你收到注释通知了吗?你不需要更改它的作用域。您可以将其声明为类的成员,但仍可以在onCreate方法中定义它。如果该数组现在不在我的类中的其他方法的范围内,是否有方法可以使用我的类中的方法访问该数组?是否收到注释通知?