2017-03-10 38 views
0

我有來自用戶的輸入得到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); 
} 

`

+1

什麼樣的錯誤?請將這些信息添加到問題中。並且:花一些時間來適當地格式化你的源代碼,而不是把這個爛攤子放在其他人身上。 – GhostCat

+0

會做比較遺憾的是 –

+0

然後你想谷歌「什麼是一個NullPointerException」 :-) – GhostCat

回答

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))); 
    } 

} 

});

+0

是的,我已經做到了,但我真正想要做的是洗牌中顯示的佈局edittexts時,隨機播放按鈕被點擊。我嘗試獲取文本並將其保存在數組中,然後用文本填充數組列表。隨機播放數組列表,然後再次使用混洗值顯示edittext。 –

+0

我已經解決了這個問題。謝謝大家試圖回答 –