我有一个编辑文本,按 ENTER 键我想调用一个函数并转到下一行,我想在按 Enter 后获得当前行的行号。
我在用edittext.onKeyListner
scene.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
String s = scene.getText().toString();
s= s + "\n";
scene.setText(s);
final int lineno = getCurrentCursorLine(scene);
//Log.v("lineno" , Integer.toString(lineno));
return true;
}
return false;
}
});
我正在尝试使用此功能获取行号
public int getCurrentCursorLine(EditText editText)
{
int selectionStart = Selection.getSelectionStart(editText.getText());
Layout layout = editText.getLayout();
if (!(selectionStart == -1)) {
return layout.getLineForOffset(selectionStart);
}
return -1;
}
问题是,如果我不调用edittext.onKeyListner,那么它会给出正确的行号,但如果我调用edittext.onKeyListner,它会返回异常,请帮忙
这是我的日志猫
10-28 13:29:22.997: E/AndroidRuntime(20220): FATAL EXCEPTION: main
10-28 13:29:22.997: E/AndroidRuntime(20220): java.lang.NullPointerException
10-28 13:29:22.997: E/AndroidRuntime(20220): at com.example.screenwritter.Newscene.getCurrentCursorLine(Newscene.java:150)
10-28 13:29:22.997: E/AndroidRuntime(20220): at com.example.screenwritter.Newscene$2.onKey(Newscene.java:126)
10-28 13:29:22.997: E/AndroidRuntime(20220): at android.view.View.dispatchKeyEvent(View.java:5558)
10-28 13:29:22.997: E/AndroidRuntime(20220): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1349)
10-28 13:29:22.997: E/AndroidRuntime(20220): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1349)
10-28 13:29:22.997: E/AndroidRuntime(20220): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1349)
10-28 13:29:22.997: E/AndroidRuntime(20220): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1349)
10-28 13:29:22.997: E/AndroidRuntime(20220): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1884)
10-28 13:29:22.997: E/AndroidRuntime(20220): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1366)
10-28 13:29:22.997: E/AndroidRuntime(20220): at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
10-28 13:29:22.997: E/AndroidRuntime(20220): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1811)
10-28 13:29:22.997: E/AndroidRuntime(20220): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3331)
10-28 13:29:22.997: E/AndroidRuntime(20220): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2601)
10-28 13:29:22.997: E/AndroidRuntime(20220): at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 13:29:22.997: E/AndroidRuntime(20220): at android.os.Looper.loop(Looper.java:137)
10-28 13:29:22.997: E/AndroidRuntime(20220): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-28 13:29:22.997: E/AndroidRuntime(20220): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 13:29:22.997: E/AndroidRuntime(20220): at java.lang.reflect.Method.invoke(Method.java:511)
10-28 13:29:22.997: E/AndroidRuntime(20220): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-28 13:29:22.997: E/AndroidRuntime(20220): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-28 13:29:22.997: E/AndroidRuntime(20220): at dalvik.system.NativeStart.main(Native Method)