循环遍历一系列edittexts并获取每个

时间:2017-03-10 08:16:38

标签: java android

我有一个自动生成的edittexts列表,来自用户输入。我想要做的是在单击随机播放按钮时将edittexts改组或获取文本并将它们设置为不同的edittexts。我尝试做的是获取每个edittext的文本,将其添加到arraylist并将其随机播放,然后使用随机列表重新创建布局。但这本身就是给我错误的。单击随机按钮时,它会给我这个错误                                                                               java.lang.NullPointerException:尝试调用虚方法' android.text.Editable android.widget.EditText.getText()'在null对象引用上 谢谢您的帮助 `

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnForCreate = (Button) findViewById(R.id.btnCreateTxt);
    editTextForInputToCreate = (EditText) findViewById(R.id.textForInputToCreate);
    listLayout = (LinearLayout) findViewById(R.id.listLayout);
    btnDisplay = (Button) findViewById(R.id.btnDisplay);
    btnShuffleTxt = (Button) findViewById(R.id.btnShuffleTxt);


    editTextForInputToCreate.animate().translationX(-1000f);



    btnForCreate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            bringTextInputBackOnScreen();


        }
    });


    btnDisplay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            if (editTextForInputToCreate.getText().toString().length() >= 0) {
                try {
                    listLayout.removeAllViews();

                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }


            length = Integer.parseInt(editTextForInputToCreate.getText().toString());


            for (i = 0; i < length; i++) {
                editTextCollection = new EditText[length];
                editText = new EditText(MainActivity.this);
                editText.setId(i + 1);
                editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                editText.setHint("Input" + " " + (i + 1));
                listLayout.addView(editText);
                editTextCollection[i] = editText;


            }


        }


    });


    btnShuffleTxt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            listLayout.removeAllViews();
            for (EditText gottenEditText:editTextCollection)
            {
                String gottenTexts = gottenEditText.getText().toString();
                list.add(gottenTexts);
            }

            Collections.shuffle(list);


        }


    });


}


private void bringTextInputBackOnScreen()
{
    editTextForInputToCreate.animate().translationXBy(1000f).setDuration(2000);
}

`

1 个答案:

答案 0 :(得分:0)

请在EditTextCollection中进行一些更正。

        editTextCollection = new EditText[length];// initialization outside the loop

for (i = 0; i < length; i++) {
            editText = new EditText(MainActivity.this);
            editText.setId(i + 1);
            editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            editText.setHint("Input" + " " + (i + 1));
            listLayout.addView(editText);
            editTextCollection[i] = editText;
        }


btnShuffleTxt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        listLayout.removeAllViews();
        for (EditText gottenEditText:editTextCollection)
        {
            String gottenTexts = gottenEditText.getText().toString();
            list.add(gottenTexts);
        }

    Collections.shuffle(list);

    for (EditText gottenEditText:editTextCollection)
    {
        gottenEditText.setText(list.get(editTextCollection.indexOf(gottenEditText)));
    }

}

});