如何使用MainActivity Listener消除NPE?

时间:2015-05-20 04:43:48

标签: android callback android-edittext listener

我正在尝试让MainActivity文件有一个侦听器来接收来自EditText文件的回调。当用户按下后退按钮时,将触发侦听器。由于软键盘将打开,我希望onKeyPreIme功能捕获后退按钮按下,以便它可以加载Toast并关闭软键盘。附件是两个关键文件。我知道onKeyPreIme函数不起作用,因为侦听器没有在MainActivity文件中加载Toast。请指教。

MainActivity.java文件:

import static com.example.jdw.secondscreen.ListenerEditText.KeyImeChange;

public class CardViewActivity extends AppCompatActivity {

Context context;

private ListenerEditText myListenerEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cardviewinput);

    myListenerEditText = (ListenerEditText) findViewById(R.id.CEditText);

    myListenerEditText.setKeyImeChangeListener(new KeyImeChange() {
        @Override
        public boolean onKeyIme(int keyCode, KeyEvent event) {
            Toast.makeText(context, "Test", Toast.LENGTH_LONG).show();
            return false;

        }
    });  
...

ListenerEditText.java文件:

public class ListenerEditText extends EditText {

private KeyImeChange keyImeChangeListener;

public ListenerEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void setKeyImeChangeListener(KeyImeChange listener) {
    keyImeChangeListener = listener;
}

public interface KeyImeChange {
    boolean onKeyIme(int keyCode, KeyEvent event);
}

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if(event.getAction()==KeyEvent.ACTION_UP &&
      keyCode==KeyEvent.KEYCODE_BACK) {
             if(keyImeChangeListener!= null) keyImeChangeListener.onKeyIme(keyCode, event);
    }
return super.onKeyPreIme(keyCode, event);
    }
}

Logcat输出:

05-20 04:19:44.742  29155-29155/com.example.jdw.secondscreen E/InputEventReceiver﹕ Exception dispatching input event.
05-20 04:19:44.742  29155-29155/com.example.jdw.secondscreen E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
05-20 04:19:44.742  29155-29155/com.example.jdw.secondscreen E/MessageQueue-JNI﹕ java.lang.NullPointerException
            at android.widget.Toast.<init>(Toast.java:92)
            at android.widget.Toast.makeText(Toast.java:238)
            at com.example.jdw.secondscreen.CardViewActivity$1.onKeyIme(CardViewActivity.java:31)
            at com.example.jdw.secondscreen.ListenerEditText.onKeyPreIme(ListenerEditText.java:34)
            at android.view.View.dispatchKeyEventPreIme(View.java:7060)
            at android.view.ViewGroup.dispatchKeyEventPreIme(ViewGroup.java:1339)
            at android.view.ViewGroup.dispatchKeyEventPreIme(ViewGroup.java:1339)
            at android.view.ViewGroup.dispatchKeyEventPreIme(ViewGroup.java:1339)
            at android.view.ViewGroup.dispatchKeyEventPreIme(ViewGroup.java:1339)
            at android.view.ViewGroup.dispatchKeyEventPreIme(ViewGroup.java:1339)
            at android.view.ViewGroup.dispatchKeyEventPreIme(ViewGroup.java:1339)
            at android.view.ViewGroup.dispatchKeyEventPreIme(ViewGroup.java:1339)
            at android.view.ViewRootImpl.deliverKeyEvent(ViewRootImpl.java:3511)
            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3113)
            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
            at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:125)
            at android.os.Looper.loop(Looper.java:124)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
05-20 04:19:44.742  29155-29155/com.example.jdw.secondscreen D/AndroidRuntime﹕ Shutting down VM
05-20 04:19:44.742  29155-29155/com.example.jdw.secondscreen W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa6263288)
05-20 04:19:44.742  29155-29155/com.example.jdw.secondscreen E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at android.widget.Toast.<init>(Toast.java:92)
            at android.widget.Toast.makeText(Toast.java:238)
            at com.example.jdw.secondscreen.CardViewActivity$1.onKeyIme(CardViewActivity.java:31)
            at com.example.jdw.secondscreen.ListenerEditText.onKeyPreIme(ListenerEditText.java:34)
            at android.view.View.dispatchKeyEventPreIme(View.java:7060)
            at android.view.ViewGroup.dispatchKeyEventPreIme(ViewGroup.java:1339)
            at android.view.ViewGroup.dispatchKeyEventPreIme(ViewGroup.java:1339)
            at android.view.ViewGroup.dispatchKeyEventPreIme(ViewGroup.java:1339)
            at android.view.ViewGroup.dispatchKeyEventPreIme(ViewGroup.java:1339)
            at android.view.ViewGroup.dispatchKeyEventPreIme(ViewGroup.java:1339)
            at android.view.ViewGroup.dispatchKeyEventPreIme(ViewGroup.java:1339)
            at android.view.ViewGroup.dispatchKeyEventPreIme(ViewGroup.java:1339)
            at android.view.ViewRootImpl.deliverKeyEvent(ViewRootImpl.java:3511)
            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3113)
            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
            at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:125)
            at android.os.Looper.loop(Looper.java:124)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

下面:

Toast.makeText(context, "Test", Toast.LENGTH_LONG).show();
               ^^^^^

contextnull

在传递给context方法之前初始化makeText

context=CardViewActivity.this;

或直接使用CardViewActivity.this来显示Toast消息,而不是使用不必要的变量:

Toast.makeText(CardViewActivity.this, "Test", Toast.LENGTH_LONG).show();