Android:EditText 上的 setOnKeyListener

发布于 2025-01-03 22:23:02 字数 2523 浏览 0 评论 0原文

我的视图上有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我做我的改变 2025-01-10 22:23:03
private EditText edittext;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.<yourlayout>.xml);
    edittext = (EditText) findViewById(R.id.editText1);

    edittext.setOnKeyListener(new EditText.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {

            return false;
        }
    });
}

如果问题仍然存在,并且您仍然收到 NPE,则您的 EditText 布局 xml 中可能有其他 id。

private EditText edittext;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.<yourlayout>.xml);
    edittext = (EditText) findViewById(R.id.editText1);

    edittext.setOnKeyListener(new EditText.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {

            return false;
        }
    });
}

if the problem persists, and you still get NPE, your EditText probably have other id in your layout xml.

醉酒的小男人 2025-01-10 22:23:03

您需要

在设置 ContentView setContentView(R.layout.main); 之后

EditText edittext = (EditText) findViewById(R.id.editText1);

onCreate() 中编写,而不是在 onCreate(); 之外编写。

u need to write

after setting ContentView setContentView(R.layout.main);

EditText edittext = (EditText) findViewById(R.id.editText1);

in onCreate() and not out of onCreate();.

櫻之舞 2025-01-10 22:23:03

setContentView(R.layout.main);失踪了

setContentView(R.layout.main); is missing

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文