出于某种原因,当我尝试为按钮ButtonAddItem
定义onclick侦听器,然后尝试添加使用EditText EditTextAddItem
的调用方法时,我收到错误
Description Resource Path Location Type
EditTextAddItem cannot be resolved or is not a field
//这里的完整代码
public class MainActivity extends ActionBarActivity {
//variables
EditText EditTextAddItem;
Button ButtonAddItem;
ListView ListItems;
//array to store items
ArrayList<String> ArrItems;
ArrayAdapter<String> ArrAdapt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//define variables
EditTextAddItem = (EditText) findViewById(R.id.editTextAddItem);
ButtonAddItem = (Button) findViewById(R.id.buttonAddItem);
ListItems = (ListView) findViewById(R.id.listView);
//set arraylist with listview
ArrItems = new ArrayList<String>();
ArrAdapt = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ArrItems);
ListItems.setAdapter(ArrAdapt);
//onclick listener
ButtonAddItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
addItem(this.EditTextAddItem.getText().toString());
}
});
//AddItems to listview method
private void addItem(String item){
if(item.length() > 0){
this.ArrItems.add(item);
this.ArrAdapt.notifyDataSetChanged();
this.EditTextAddItem.setText("");
}
}
}
答案 0 :(得分:1)
您的问题是您正在尝试查找OnClickListener的成员EditTextAddItem
。你需要通过在this
前面添加父类的名称来告诉它查看父类:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
addItem(MainActivity.this.EditTextAddItem.getText().toString());
}