Java 无法解析findViewById-正在从多个字段提取数据
我无法让我的代码解析findViewById—我有一个方法可以从自定义对话框中获取两段数据,但是如果我使用findViewById尝试获取数据,我只会发现它无法解析它 见此摘录:Java 无法解析findViewById-正在从多个字段提取数据,java,android,Java,Android,我无法让我的代码解析findViewById—我有一个方法可以从自定义对话框中获取两段数据,但是如果我使用findViewById尝试获取数据,我只会发现它无法解析它 见此摘录: public class ItemsActivity extends ActionBarActivity { ArrayList<Item> itemlistv = new ArrayList<Item>(); @Override protected void onCreate(Bundle
public class ItemsActivity extends ActionBarActivity {
ArrayList<Item> itemlistv = new ArrayList<Item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_items);
populateListView();
final Button addbutton = (Button) findViewById(R.id.button_add_item);
addbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final AlertDialog.Builder alert = new AlertDialog.Builder(ItemsActivity.this);
LayoutInflater inflater = ItemsActivity.this.getLayoutInflater();
alert.setView(inflater.inflate(R.layout.dialog_additems, null))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
Editable itemname = (Editable)dialog.findViewById(R.id.itemname);
Editable itemprice = (Editable)dialog.findViewById(R.id.itemprice);
//String itemnamestring = itemname.getText().toString();
// itemlistv.add(new Item(itemname, itemprice));
populateListView();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// End of dialog
}
});
alert.show();
}
});
// etc
我只是不能让findViewById在这里工作,但我已经在我的程序中的其他课程中工作得非常好。我犯了愚蠢的错误吗
忽略这两条注释行,我知道它们不对-我的重点是为什么在这个例子中找不到findByViewId
非常感谢
Alex不在对话框中搜索视图字段。改为做下面这样的事情-
LayoutInflater inflater = ItemsActivity.this.getLayoutInflater();
final View modifyView = inflater.inflate(R.layout.dialog_additems, null);
final Editable itemname = (Editable)modifyView.findViewById(R.id.itemname);
final Editable itemprice = (Editable)modifyView.findViewById(R.id.itemprice);
alert.setView(modifyView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
//String itemnamestring = itemname.getText().toString();
// itemlistv.add(new Item(itemname, itemprice));
populateListView();
}
})
//your code
我想我已经得到了它-应该是EditableItemName=EditablefindViewByIdR.id.itemname;Editable itemprice=EditablefindViewByIdR.id.itemprice;