gpt4 book ai didi

java - Logcat onclicklistener android 错误

转载 作者:行者123 更新时间:2023-12-02 08:07:50 25 4
gpt4 key购买 nike

当我尝试运行我的脚本时,我在 logcat 中收到这样的错误。我对比了一下,就是onClickListener。有什么建议来解决这个问题吗?还是个初学者。

最佳答案

问题存在于此:

View splashscreen = (View) findViewById(R.layout.splash);
splashscreen.setOnClickListener(this);

您收到异常,因为 splashscreen 为空,并且不允许在空指针上调用 setOnClickListener()。指针为空的原因是您需要使用正确的 ID 从 XML 中获取对 View 的引用。位于 res/layout 中的 splash.xml 文件将作为 Activity 的内容 View 加载,但您应该有一个与该特定 View 关联的正确的 R.id 值.

splash.xml 中,为此目的声明的 View 应在其 XML 声明中具有 android:id="@+id/something" 属性(我从空中捡起“某物”,这个标识符可以是你想要的任何东西)。然后你可以调用:

//Hint: You don't have to cast the result if the pointer is a plain vanilla View
View splashscreen = findViewById(R.id.something);
splashscreen.setOnClickListener(this);

然后您将获得对该 View 的有效引用,并且您的 set 方法不会失败。

HTH

关于java - Logcat onclicklistener android 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7884326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com