开发者

ToDo list example: error adding task (setOnKeyListener)

开发者 https://www.devze.com 2023-03-03 06:07 出处:网络
I am following the book Professional Android development by Reto Meier, and there is an example of a ToDo list to be done in order to practice.

I am following the book Professional Android development by Reto Meier, and there is an example of a ToDo list to be done in order to practice. The problem is that I do everything as the book says, but I cannot add any task when pushing the central KeyPad, as I get an开发者_Python百科 exception and the program has to close. I tried to debug in Eclipse, and apparently it cannot find the .class file (?)

Here is the code where all the bad things are happening, specially in the todoItems.add line:

myEditText.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN)
            if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
                todoItems.add(0, myEditText.getText().toString());
                myEditText.setText("");
                aa.notifyDataSetChanged();
                return true;
            }
        return false;
    }
});


give this a shot - it uses View v that's passed in. I assume that "todoItems" is properly instatiated

myEditText.setOnKeyListener(new View.OnKeyListener()
{
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        EditText edittxt = (EditText)v;

        todoItems.add(0, edittxt.getText().toString());

        return false;
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消