我有來自用戶的輸入得到edittexts的自動生成的列表中的文本。我想要做的就是在單擊隨機播放按鈕時拖動edittext,或者獲取文本並將它們設置爲不同的edittext。我試着做的是讓每個edittext的文本添加到arraylist並洗牌,然後用混洗列表重新創建佈局。但是這本身就給了我錯誤。當單擊隨機按鈕時,它給了我這個錯誤 java.lang.NullPointerException:嘗試調用空對象上的虛擬方法'android.text.Editable android.widget.EditText.getText()'參考 感謝有關幫助 `通過edittexts陣列循環和得到的每個
@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);
}
`
什麼樣的錯誤?請將這些信息添加到問題中。並且:花一些時間來適當地格式化你的源代碼,而不是把這個爛攤子放在其他人身上。 – GhostCat
會做比較遺憾的是 –
然後你想谷歌「什麼是一個NullPointerException」 :-) – GhostCat