我在这里要做的是在软键上按“Enter / Done”按下EditText中的当前单词。但是,每次按下软键上的“Enter / Done”,应用程序都会崩溃。
我尝试进行一些调试,似乎问题似乎是 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
另外,你能帮我理解为什么在实际从EditText中获取字符串是通过onEditorAction函数中的TextView实例获取的吗?
[UPDATE]
这是代码的声明部分....
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 :(得分:2)
是的怀疑,ArrayAdapter
支持array
。
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);
}