gpt4 book ai didi

java - 从类方法访问 onCreateView() 中声明的变量

转载 作者:行者123 更新时间:2023-12-01 23:10:08 24 4
gpt4 key购买 nike

如果我有在片段的 onCreateView 中声明的变量;有没有办法可以从 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);


centralDigitalTable = (TableLayout) digitalfragmentview
.findViewById(R.id.digitalTable);

Switch[] digitalIOModeSwitchArray = {digitalIO0Mode, digitalIO1Mode,
digitalIO2Mode, digitalIO3Mode, digitalIO4Mode, digitalIO5Mode,
digitalIO6Mode, digitalIO7Mode, digitalIO8Mode, digitalIO9Mode};


return digitalfragmentview;
}

现在我在类中有这些方法:

   public Switch getIOModeSwitch(int index) {
return digitalIOModeSwitchArray[index];
}

public void getIOModeSwitch(int index, boolean status) {
digitalIOModeSwitchArray[index].setActivated(status);
}

但是;他们显然无法访问 digitalIOModeSwitchArray,因为它超出了他们的范围并本地化到 onCreateView 方法。

我尝试将所有变量的启动放在类的 header 之后,但是如果我这样做,数组似乎会填充 null,所以我显然不能这样做。

编辑1:

例如,

如果我这样做 -

public class digitalFragment extends FragmentActivity{
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);
Switch[] digitalIOModeSwitchArray = {digitalIO0Mode, digitalIO1Mode,
digitalIO2Mode, digitalIO3Mode, digitalIO4Mode, digitalIO5Mode,
digitalIO6Mode, digitalIO7Mode, digitalIO8Mode, digitalIO9Mode};


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



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

}

logcat 输出如下所示:

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
Array is null at: 6
Array is null at: 7
Array is null at: 8
Array is null at: 9

最佳答案

不可以,您不能从声明范围之外访问局部变量。

您必须将它们声明为类中的成员字段。查看官方教程Member Variables .

class Example {

int field;
int hello = 2; // initialized to 2
int there;
Object[] array = new Object[50]; // array initialized here, all elements null

Example () {
there = 2; // can also be initialized in constructor
// initialize each element of array:
for (int n = 0; n < array.length; ++ n)
array[n] = new Object();
}

void a () {
int local;
local = 1; // ok
field = 1; // ok
hello = 1; // ok
there = 1; // ok
}

void b () {
System.out.println(local); // no 'local' declared here
System.out.println(field); // ok
System.out.println(hello); // ok
System.out.println(there); // ok
}

}

编辑:扩展示例回复:评论和编辑以发布。

关于java - 从类方法访问 onCreateView() 中声明的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22152219/

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