2013-07-06 22 views
0

我在這裏要做的是在軟鍵「輸入/完成」按下時在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); 

    } 
+0

什麼是適配器的類? –

+0

是的,什麼是適配器類?還請檢查您的LogCat的堆棧跟蹤,這是要讀取的導入日誌!會告訴你確切的錯誤 – Blundell

+0

'EditText'是'TextView'的一個子類http://developer.android.com/reference/android/widget/EditText.html – Blundell

回答

2

呀爲疑似,你ArrayAdapterarray支持。

Array不支持添加操作。

將其更改回ArrayList。即:

private final static List<String> items = new ArrayList(); 
static { 
    items.add("this"); 
    items.add("is"); 
    items.add("a"); 
    items.add("really"); 
    items.add("silly"); 
    items.add("list"); 
} 

由於WandMaker暗示,你似乎已經做了一些工作,已經創建一個ArrayList,另一種解決方案可能是:

adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, words); 

的最佳方案(與您代碼):

public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     List<String> words = new ArrayList<String>(); 
     words.add("this"); 
     words.add("is"); 
     words.add("a"); 
     words.add("really"); 
     words.add("silly"); 
     words.add("list"); 
     adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, words); 
     setListAdapter(adapter); 
    } 
+0

我不明白你的答案。它在哪裏支持數組?他有單詞= new ArrayList (); –

+0

@WandMaker不需要-1因爲*你*沒有得到它。在他的問題中有一個數組支持:'ArrayAdapter (this,android.R.layout.simple_list_item_1,items);' – Blundell

+0

但是您的解決方案不必要求創建一個靜態變量。你可以通過告訴他將「單詞」作爲參數而不是「我已經刪除-1」的「項目」來解決答案,但是你的解決方案並不正確。 –

相關問題