title: Unable to get line number on press of ENTER
body: <p>I have an edittext, on press of ENTER key i want to call a function and go to next line, and i want to have the line number of my current line after pressing enter.</p>
  
  <p>I am using <code>edittext.onKeyListner</code></p>
  
  <pre><code> 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) &amp;&amp;
                      (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;
          }
  
      });
  </code></pre>
  
  <p>And i am trying to get line number using this function</p>
  
  <pre><code>public int getCurrentCursorLine(EditText editText)
  {    
      int selectionStart = Selection.getSelectionStart(editText.getText());
      Layout layout = editText.getLayout();
  
      if (!(selectionStart == -1)) {
          return layout.getLineForOffset(selectionStart);
      }
  
      return -1;
  }
  </code></pre>
  
  <p>The problem is, if i do not call edittext.onKeyListner, then it will give the right line number but if i call edittext.onKeyListner it will return exception
  please help</p>
  
  <p>here is my logcat</p>
  
  <pre><code>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)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/Leabz.png" alt="enter image description here"></p>