我的微调器的onItemSelected函数没有识别活动中的变量(声明为final)。对于识别这些相同变量的按钮,我在同一活动中有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无法解析'
答案 0 :(得分: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;
}
}