我在這裏要做的是在軟鍵「輸入/完成」按下時在EditText中添加當前字。但是,每按一次軟鍵上的「輸入/完成」,應用程序就會崩潰。UnsupportedOperationException在通過ArrayAdapter向列表中添加字符串時出現的問題
我試過做一些調試,看來問題似乎是adapter.add(v.getText()。toString());一行。我不知道爲什麼/如何!
public boolean onEditorAction(TextView v,int actionId, KeyEvent event) {
Log.d("InThere","in onEditorAction1");
if((event==NULL) || (event.getAction()==KeyEvent.ACTION_UP))
{
if(event==NULL)
Log.d("InThere","inside if+EVENT");
if(event.getAction()==KeyEvent.ACTION_UP)
Log.d("InThere","inside if+ACTION_UP");
Log.d("InThere","before adapter ");
adapter.add(v.getText().toString()); <<<cause of error ?
Log.d("InThere","after adapter ");
v.setText("");
InputMethodManager imm=(InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
Log.d("InThere","in onEditorAction2");
return true;
}
我創建了標籤「InThere」過濾器和這裏的是什麼樣子的logcat的:
InThere in onEditorAction1
InThere in onEditorAction2
InThere in onEditorAction1
InThere inside if+ACTION_UP
InThere before adapter
而且,你能幫助我理解爲什麼字符串是通過onEditorAction函數中的TextView實例獲得的,實際上它是從EditText實際獲得的?
[更新]
下面的代碼的聲明部分....
private final static String[] items={"this","is","a","really","silly","list"};
private static final KeyEvent NULL = null;
private ArrayList<String> words=null;
private ArrayAdapter<String> adapter;
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
words = new ArrayList<String>();
for(String s: items)
{
words.add(s);
}
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
setListAdapter(adapter);
}
什麼是適配器的類? –
是的,什麼是適配器類?還請檢查您的LogCat的堆棧跟蹤,這是要讀取的導入日誌!會告訴你確切的錯誤 – Blundell
'EditText'是'TextView'的一個子類http://developer.android.com/reference/android/widget/EditText.html – Blundell