所以我試圖爲二十一點創建卡片計數應用程序,但是當我嘗試設置某人正在玩的套牌的數量時,抓住它從可編輯的editText程序似乎崩潰。具體來說,它說「不幸」我的應用程序「已停止。」當我嘗試從EditText獲取文本並將其轉換爲Integer時,應用程序崩潰
這是我所做的代碼。
public int numberOfDecks;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityName)
final EditText numberField = (EditText) findViewById(R.id.editNumber);
Button button = (Button) findViewById(R.id.buttonDecks);
button.setOnClickListener(new OnclickListener() {
public void onClick(View arg0) {
numberOfDecks = Integer.parseInt(numberField.getText().toString());
//Because this didn't work I also tried doing
//numberOfDecks = Integer.getInteger(numberField.getText().toString());
//However both yield the same problem
}
}
}
起初我以爲它與上點擊的方法和解析INT,或可能與一個問題做一個空白的EditText場然而,這的onClick發生在編輯已editted後用。接下來,我嘗試將它放在代碼塊後點擊,同樣的結果仍然發生,但之前顯然這停止,因爲EditText是空的。
編輯:EditText上盒是基於數量的EditText框,所以唯一的有效輸入是一個數字
可能是編輯文本值不能轉換爲整數,如果你想要用戶類型編號,你可以將edittext的inputtype設置爲編號。所以從Android操作系統顯示用戶的數字鍵盤 –
的logcat請詳查 – Siddharth
要清理這樣的誤解,這是的EditText基於數量的EditText所以唯一有效的輸入是跟蹤你,但在真正困難的人一些 –