2

我试图有一个功能,当按下按钮时,布局被添加到我的视图中。布局中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 )。EditTextgetLayout

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++)

4

2 回答 2

5

尝试:

.......
View someView = inflater.inflate(R.layout.get_param, view, false);

EditText nameBox = (EditText) someView.findViewById(R.id.getValue);
EditText valueBox = (EditText) someView.findViewById(R.id.getName);
........ 
于 2012-07-19T14:39:44.297 回答
1
View view = inflater.inflate(R.layout.get_param, view, false);

Inflater 会膨胀,但不会将膨胀的结果分配给任何视图对象。

于 2012-07-19T14:38:02.530 回答