我试图在用户按下键盘上的“下一步”时执行某些功能,但我遇到了一些问题。应用程序编译,但按下“下一步”后崩溃。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText etGhs = (EditText) findViewById(R.id.etGhs);
// Calculate DGM once the user has finished typing
etGhs.setOnEditorActionListener(
new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (!event.isShiftPressed()) {
// the user is done typing.
// get string from etGhs
double ghs = Integer.parseInt(etGhs.getText().toString());
double dgm = ghs * 0.000064179; // calculate avg DGM
setDgmAndDailyBtc(dgm);
return true; // consume.
}
}
return false; // pass on to other listeners.
}
});
}
这是logcat
03-11 20:51:09.847 11528-11528/com.devinshoe.bitcoinprofit.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.devinshoe.bitcoinprofit.app, PID: 11528
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.KeyEvent.getAction()' on a null object reference
at com.devinshoe.bitcoinprofit.app.MainActivity$1.onEditorAction(MainActivity.java:27)
at android.widget.TextView.onEditorAction(TextView.java:4236)
at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:297)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
提前感谢您的帮助。
答案 0 :(得分:0)
一个老问题,但如果您仍在寻找答案,以下内容对我有用。您检查的部分"输入"选项,即
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
event.getAction().....
您需要添加更多搜索选项,即
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
actionId == EditorInfo.IME_ACTION_GO ||
actionId == EditorInfo.IME_ACTION_NEXT ||
event.getAction().....
我的键盘设置为将默认操作设为IME_ACTION_GO,但为了更加安全,我将其他选项保留在那里。
答案 1 :(得分:-1)
事件为空。您需要检查并且不要在其上调用getAction