Method in Main program doesn't work when moved to a class

时间:2016-11-03 15:41:05

标签: android

Beginner android question: I have a method in a Main program that works just fine:

 setRadioButtons(answer, value);

... void setRadioButtons (int answer, int value) {
    if (answer == 1)
        ((RadioButton) findViewById(R.id.C1Button)).setChecked(true);
    else if (answer == 2) ....

However this should really be in a class. When I move it to a class it fails:

Context context= getApplicationContext();
myQ1.setRadioButtons(answer, value, context);

 ...    public void setRadioButtons(int answer, int value, int context) {
        setContentView(context);
        if (answer == 1)
          ((RadioButton) findViewById(R.id.C1Button)).setChecked(true);

The method call in the Main program shows context underlined in red with the warning:

Wrong 3rd argument type. Found: 'android.content.Context', required: 'int'

This must have something to do with context but I really can't figure it out.

3 个答案:

答案 0 :(得分:1)

public void setRadioButtons(int answer, int value, int context)

期望参数int, int, int。您提供的是int, int, Context

将方法签名更改为int, int, context,如下所示:

public void setRadioButtons(int answer, int value, Context context)

答案 1 :(得分:1)

Context拥有它自己的类。

无论如何,你应该传递给

setContentView

不是上下文,而是布局资源ID,如

R.layout.example

答案 2 :(得分:0)

这是因为你已将上下文声明为int。实际上它应该是方法中的Context实例。改变

  public void setRadioButtons(int answer, int value, Context context)
相关问题