如何访问内部类Android中的变量

时间:2021-10-27 23:47:29

I've created programmatically 4 radio groups. The problem comes, when i try to use setOnClickListener through an inner class, that needs access to the iteration variable (i) of the loop used to declare the radio groups. I tried also to have the iterator variable final, but it didn't work. I need to set all 4 radio groups clearCheck(). Here is my code:

我已经以编程方式创建了4个无线电组。问题来了,当我尝试通过内部类使用setOnClickListener时,需要访问用于声明无线电组的循环的迭代变量(i)。我也尝试将迭代器变量设为final,但它不起作用。我需要设置所有4个无线电组clearCheck()。这是我的代码:

public class MainActivity extends AppCompatActivity {

    **RadioGroup[] radioGroup;**

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup = new RadioGroup[4];
        for (int i = 0; i < 4; i++) {
            radioGroup[i] = new RadioGroup(this);

            finishButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    **radioGroup[i].clearCheck();**
                }
            });
            linearLayout.addView(radioGroup[i]);
        }
    }
}

The error is: local variable i is accessed from within inner class; needs to be declared final
Thanks!

错误是:从内部类中访问局部变量i;需要申报最后谢谢!

1 个解决方案

#1


5  

For the first problem: local variable i is accessed from within inner class; needs to be declared final (or how to access to a variable within inner class in the title), you can clear this problem just by copying value of i to another final valiable (ex. j). Then use the copied one in the inner class.

对于第一个问题:从内部类中访问局部变量i;需要被声明为final(或者如何访问标题中内部类中的变量),您可以通过将i的值复制到另一个最终的valiable(例如j)来清除此问题。然后在内部类中使用复制的那个。

final int j = i;
finishButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        radioGroup[j].clearCheck();
    }
});

This technique is frequently used.

经常使用这种技术。


For the next problem: clearCheck() is applied only to the 4th radio group. This is because you can set a single OnClickListener to the finishButton at once. That is the last set one for the 4th group was only valid.

对于下一个问题:clearCheck()仅应用于第4个无线电组。这是因为您可以一次将单个OnClickListener设置为finishButton。这是第四组的最后一组仅有效。

So you should set only a single OnClickListener, and in the listener you can invoke clearCheck for all of RadioGroups. Then below will solve your problem.

因此,您应该只设置一个OnClickListener,并且在侦听器中,您可以为所有RadioGroup调用clearCheck。然后下面将解决您的问题。

radioGroup = new RadioGroup[4];
for (int i = 0; i < 4; i++) {
    radioGroup[i] = new RadioGroup(this);
    linearLayout.addView(radioGroup[i]);
}

finishButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        for (int i = 0; i < 4; i++) {
            radioGroup[i].clearCheck();
        }
    }
});

Note that so this solution clears your problem more basically that clears local variable i is accessed from within inner class; needs to be declared final error, so the first solution which I suggested above will be no more needed. You don't need to access to a variable within inner class in the title any more.

请注意,这个解决方案更基本上清除了你的问题,清除了从内部类中访问的局部变量;需要声明最终错误,因此我不再需要上面建议的第一个解决方案。您不需要再访问标题中内部类中的变量。

#1


5  

For the first problem: local variable i is accessed from within inner class; needs to be declared final (or how to access to a variable within inner class in the title), you can clear this problem just by copying value of i to another final valiable (ex. j). Then use the copied one in the inner class.

对于第一个问题:从内部类中访问局部变量i;需要被声明为final(或者如何访问标题中内部类中的变量),您可以通过将i的值复制到另一个最终的valiable(例如j)来清除此问题。然后在内部类中使用复制的那个。

final int j = i;
finishButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        radioGroup[j].clearCheck();
    }
});

This technique is frequently used.

经常使用这种技术。


For the next problem: clearCheck() is applied only to the 4th radio group. This is because you can set a single OnClickListener to the finishButton at once. That is the last set one for the 4th group was only valid.

对于下一个问题:clearCheck()仅应用于第4个无线电组。这是因为您可以一次将单个OnClickListener设置为finishButton。这是第四组的最后一组仅有效。

So you should set only a single OnClickListener, and in the listener you can invoke clearCheck for all of RadioGroups. Then below will solve your problem.

因此,您应该只设置一个OnClickListener,并且在侦听器中,您可以为所有RadioGroup调用clearCheck。然后下面将解决您的问题。

radioGroup = new RadioGroup[4];
for (int i = 0; i < 4; i++) {
    radioGroup[i] = new RadioGroup(this);
    linearLayout.addView(radioGroup[i]);
}

finishButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        for (int i = 0; i < 4; i++) {
            radioGroup[i].clearCheck();
        }
    }
});

Note that so this solution clears your problem more basically that clears local variable i is accessed from within inner class; needs to be declared final error, so the first solution which I suggested above will be no more needed. You don't need to access to a variable within inner class in the title any more.

请注意,这个解决方案更基本上清除了你的问题,清除了从内部类中访问的局部变量;需要声明最终错误,因此我不再需要上面建议的第一个解决方案。您不需要再访问标题中内部类中的变量。