What I am trying to do here is add the word currently in the EditText at the press of "Enter/Done" on the softkey. However, each time I press the "Enter/Done" on the soft key, the app crashes.
I've tried to do some debugging and it appears that the issue seems to be the adapter.add(v.getText().toString()); line. I am not sure why/how !!
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;
}
I created a filter with the tag "InThere" and here's what it looks like in the LogCat:
InThere in onEditorAction1
InThere in onEditorAction2
InThere in onEditorAction1
InThere inside if+ACTION_UP
InThere before adapter
Also, could you help me understand why the string is being obtained through an instance of TextView in the onEditorAction function when actually it is actually being obtained from EditText ?
[UPDATE]
Here's the declaration part of the code....
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);
}
EditTextis a subclass ofTextViewdeveloper.android.com/reference/android/widget/EditText.htmlprivate ArrayAdapter<String> adapter;within the same class i.e MainActivity.javaArrayListcontaining the items values and pass that to theArrayAapter. Otherwise the adapter will create a non mutable list from the array you pass in for which theadd/removemethods don't work and throw the error you see.