Android:EditText 上的 setOnKeyListener
我的视图上有一个 EditText,我想设置一个 onKeyListener。
这是我的代码:
final EditText edittext = (EditText) findViewById(R.id.editText1);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
edittext.setOnKeyListener(new EditText.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
return false;
}
});
}
问题是我对此有问题,这是 logcat 的错误部分:
W/dalvikvm(3703): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime(3703): FATAL EXCEPTION: main
E/AndroidRuntime(3703): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{milos.mdpi/milos.mdpi.MDPIActivity}: java.lang.NullPointerException
E/AndroidRuntime(3703): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
E/AndroidRuntime(3703): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime(3703): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime(3703): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime(3703): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(3703): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(3703): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(3703): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(3703): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(3703): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(3703): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(3703): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(3703): Caused by: java.lang.NullPointerException
E/AndroidRuntime(3703): at android.app.Activity.findViewById(Activity.java:1647)
E/AndroidRuntime(3703): at milos.mdpi.MDPIActivity.<init>(MDPIActivity.java:22)
E/AndroidRuntime(3703): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(3703): at java.lang.Class.newInstance(Class.java:1409)
E/AndroidRuntime(3703): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime(3703): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
E/AndroidRuntime(3703): ... 11 more
I have a EditText on my View and I would like to set a onKeyListener.
Here is my code:
final EditText edittext = (EditText) findViewById(R.id.editText1);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
edittext.setOnKeyListener(new EditText.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
return false;
}
});
}
The problem is that i have problems with this, here is a error part of the logcat:
W/dalvikvm(3703): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime(3703): FATAL EXCEPTION: main
E/AndroidRuntime(3703): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{milos.mdpi/milos.mdpi.MDPIActivity}: java.lang.NullPointerException
E/AndroidRuntime(3703): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
E/AndroidRuntime(3703): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime(3703): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime(3703): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime(3703): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(3703): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(3703): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(3703): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(3703): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(3703): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(3703): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(3703): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(3703): Caused by: java.lang.NullPointerException
E/AndroidRuntime(3703): at android.app.Activity.findViewById(Activity.java:1647)
E/AndroidRuntime(3703): at milos.mdpi.MDPIActivity.<init>(MDPIActivity.java:22)
E/AndroidRuntime(3703): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(3703): at java.lang.Class.newInstance(Class.java:1409)
E/AndroidRuntime(3703): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime(3703): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
E/AndroidRuntime(3703): ... 11 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果问题仍然存在,并且您仍然收到 NPE,则您的 EditText 布局 xml 中可能有其他 id。
if the problem persists, and you still get NPE, your EditText probably have other id in your layout xml.
您需要
在设置 ContentView
setContentView(R.layout.main);
之后在
onCreate()
中编写,而不是在onCreate();
之外编写。u need to write
after setting ContentView
setContentView(R.layout.main);
in
onCreate()
and not out ofonCreate();
.setContentView(R.layout.main);失踪了
setContentView(R.layout.main); is missing