0

我的微调器的 onItemSelected 函数无法识别活动中的变量(声明为最终变量)。对于识别这些相同变量的按钮,我在同一活动中有 onClickListener 函数。

这是一些代码:

public class Currency extends Activity implements OnItemSelectedListener{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_currency);
    final TextView tv2 = (TextView) findViewById(R.id.textView2);
    final TextView tv3 = (TextView) findViewById(R.id.textView3);

}

@Override
public void onItemSelected(AdapterView<?> arg0, View v, int position,
        long arg3) {
    switch (position) {
    case 0:
        tv2.setText("something");
        tv3.setText("something");
        break;
    case 1:
        tv2.setText("something");
        tv3.setText("something");
    case 2:
        tv2.setText("something");
        tv3.setText("something");
        break;

        default:
        break;
    }

}

错误(每一行旁边):'tv2 无法解析'

4

1 回答 1

1

在类级别声明 tv2,如下所示。从 onCreate 中删除声明

private TextView tv2

您的代码必须如下所示

public class Currency extends Activity implements OnItemSelectedListener{
 private TextView tv2;
private TextView tv3;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_currency);
     tv2 = (TextView) findViewById(R.id.textView2);
     tv3 = (TextView) findViewById(R.id.textView3);

}


@Override
public void onItemSelected(AdapterView<?> arg0, View v, int position,
        long arg3) {
    switch (position) {
    case 0:
        tv2.setText("something");
        tv3.setText("something");
        break;
    case 1:
        tv2.setText("something");
        tv3.setText("something");
    case 2:
        tv2.setText("something");
        tv3.setText("something");
        break;

        default:
        break;
    }

}
于 2013-01-16T16:39:57.687 回答