我试图有一个功能,当按下按钮时,布局被添加到我的视图中。布局中2 的 IDEditText
被添加到 aList<EditText>
中,因此无论添加多少 XML 布局实例,我都可以访问它们。但是,我无法让它发挥作用。
设置视图的主要功能:
ViewGroup view = null;
TextView addressView;
Button submit;
ImageView addHeader;
private int id = 0;
private List<EditText> editTexts = new ArrayList<EditText>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get);
view = (ViewGroup) findViewById(R.id.getLayout);
addressView = (TextView) view.findViewById(R.id.getAddress);
submit = (Button) view.findViewById(R.id.getSubmit);
addHeader = (ImageView) view.findViewById(R.id.getHeaderAdd);
addHeader.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
addHeaderBox();
}
});
我的功能添加布局:
private void addHeaderBox() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.get_param, view, false);
EditText nameBox = (EditText) view.findViewById(R.id.getValue);
EditText valueBox = (EditText) view.findViewById(R.id.getName);
nameBox.setId(id++);
valueBox.setId(id++);
editTexts.add(nameBox);
editTexts.add(valueBox);
}
getValue
并且是我布局getName
中两个 s 的 ID (不是 main )。EditText
getLayout
07-19 10:25:13.088: E/AndroidRuntime(32034): java.lang.NullPointerException
07-19 10:25:13.088: E/AndroidRuntime(32034): at myapp.httprequest.free.GETActivity.addHeaderBox(GETActivity.java:96)
07-19 10:25:13.088: E/AndroidRuntime(32034): at myapp.httprequest.free.GETActivity.access$0(GETActivity.java:88)
07-19 10:25:13.088: E/AndroidRuntime(32034): at myapp.httprequest.free.GETActivity$1.onClick(GETActivity.java:65)
第 96 行是nameBox.setId(id++)