尝试在空对象引用上调用虚拟方法 -> 解析?

2024-05-21

我尝试使用一个按钮从一个主要活动切换到另一个活动,但是当我尝试运行该应用程序时,它在单击该应用程序后立即自行关闭。我在命令中发现错误消息:Unable to start activity ComponentInfo{fr.amseu.mystretching/fr.amseu.mystretching.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 我在 MainActivity 中的代码是这样的:


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button = findViewById<ImageButton>(R.id.imageButton)

        button.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent);} 

这是我的主要活动:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/item_main"
    android:background="@color/light_red">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="100dp"
        android:layout_height="120dp"
        app:cardCornerRadius="8dp"
        app:cardElevation="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/lower_legs"
            android:contentDescription="@string/home_page_first_button" />

    </androidx.cardview.widget.CardView>

    <View
        android:id="@+id/view_separation"
        android:layout_width="match_parent"
        android:visibility="invisible"
        android:layout_height="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/name_item"
        style="@style/SubTitleTextStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/default_margin"
        android:text="@string/home_page_first_button"
        app:layout_constraintBottom_toTopOf="@+id/view_separation"
        app:layout_constraintStart_toEndOf="@+id/cardView"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/description_item"
        style="@style/DefaultTextStyle"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="@string/home_page_description_1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/name_item"
        app:layout_constraintTop_toBottomOf="@+id/view_separation" />



</androidx.constraintlayout.widget.ConstraintLayout>

对于我的回收器视图,我在页面中采用此模型,以下项目是同级项目,但我使用适配器 HomeFragment 更改图像和文本... 有人能帮助我吗 ? (这是我的第一个应用程序) 多谢 !


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试在空对象引用上调用虚拟方法 -> 解析? 的相关文章

随机推荐

  • 如何使用cleartool获取UCM流和基线?

    对于构建日志记录 我需要获取当前流 基线cleartool 但我一直在确定哪个命令将为我提供此信息 我怎样才能使cleartool告诉我我当前正在查看哪个流 基线 如果输出需要预处理或过滤也没有问题 如果您在视图中 您可以 获取当前流 cl
  • Django 与谷歌图表

    我试图让谷歌图表显示在我的页面上 但我不知道如何将值从 django 视图传递到 javascript 以便我可以绘制图表 姜戈代码 array Year Sales Expenses 2004 1000 400 2005 1170 460
  • span 和 iframe 正文中的宽度(以像素为单位)

    我需要知道 a 的宽度 nbsp 以像素为单位 以及是否取决于字体大小 另外 页面中不同元素的情况是否有所不同 还有 就是 nbsp 与常规不同 目的 nbsp 不间断空格 位于正常空格之上是为了防止单词之间出现换行 您可以使用多个 nbs
  • 将 Swagger 与命名空间版本的 WebApi 结合使用

    我已经找到了如何使用基于名称空间的 WebAPI 版本这个班 https aspnet codeplex com SourceControl changeset view dd207952fa86 Samples WebApi Namesp
  • 如何跟踪hadoop中哪个数据块在哪个数据节点?

    如果复制一个数据块 会复制到哪个数据节点 是否有任何工具可以显示复制块存在的位置 如果您知道文件名 则可以通过 DFS 浏览器查找 转到您的 namenode Web 界面 说 浏览文件系统 并导航到您感兴趣的文件 在页面底部 将列出文件中
  • 将 char 绑定到枚举类型

    我有一段与此非常相似的代码 class someclass public enum Section START MID END vector section Full void ex for int i 0 i section
  • 接口中“不能降低继承方法的可见性”的含义

    我有两个文件 public interface PrintService void print PrintDetails details class PrintDetails private String printTemplate pub
  • Application.onLowMemory() 未调用

    我创建了自己的应用程序类 我尝试调试它 代码在 Application onCreate 处停止 但不会在 onLowMemory 处停止 为了测试该场景 我打开了许多其他高内存应用程序 我看到的是调试会话终止 在 Eclipse 中 并且
  • MVVM 消息传递或事件或其他什么选项?

    我在 MainViewModel 中有一个菜单 现在在选择特定的菜单项时我想更新已加载的视图的数据 即 虽然 MainViewModel 中有该 viewModel 的实例 但当我尝试通过该实例调用该方法并更改数据属性时 它不会显示视图中的
  • 我可以使用特征中的方法重写 scala 类方法吗?

    class PasswordCaseClass val password String trait PasswordTrait self PasswordCaseClass gt override def password blue val
  • 通过向上转换将 Java.sql.date 转换为 Java.util.date 安全吗?

    java sql date 扩展了 java util date 那么通过将 java sql date 转换为 java util date 是否可以在两者之间进行转换 或者有其他方法可以转换它们吗 您不一定需要强制转换 您可以将 SQL
  • 如何修复超出最大调用堆栈大小

    有一个 MERN Firebase 应用程序并收到此错误和一堆 atdeepExtend deepCopy ts 71 RangeError Maximum call stack size exceeded getApps as apps
  • pytesseract 无法从图像中识别复杂的数学公式

    我在用pytesseractpython 中的模块 pytesseract从图像中识别文本 但它不适用于包含复杂数学公式 例如根 推导 积分数学问题或方程 的图像 代码2 py Import modules from PIL import
  • 运行测试项目时自动启动ASP.MVC项目

    我正在尝试为我的 ASP 网站设置一个测试项目 对于某些测试 我想使用 selenium 来执行端到端测试 因此 我的网站必须运行 以便测试可以访问该网站 运行测试时如何启动我的网站项目 请参考以下链接 我相信这是可能的 但会有点棘手 这些
  • AES 密钥是随机的吗?

    AES 密钥可以通过此代码生成 KeyGenerator kgen KeyGenerator getInstance AES kgen init 128 but 如果我有一个 非常可靠 的生成随机数的方法 我可以这样使用它吗 SecureR
  • 如何绘制 Voronoi 曲面细分的多边形而不是线段?

    我找到了一种使用 ggplot2 绘制 Voronoi 曲面细分的分段的方法 library deldir library ggplot2 library ggthemes set seed 123 df lt data frame lat
  • 无法将共享项目添加到 Visual Studio 测试项目

    我正在使用 Visual Studio 2015 我有一个作为独立解决方案的共享项目 其他几个解决方案中使用的扩展的集合 我想测试共享项目 独立于任何其他解决方案 因此 我添加了一个新的测试项目 但是 在我的测试项目中 我无法添加对共享项目
  • 符合 POSIX 标准的 shell 相当于 Bash“while read -d $'\0' ...”?

    我正在尝试使 Bash 脚本严格符合 POSIX 标准 即消除任何潜在的 Bashisms http mywiki wooledge org Bashism 通过使用checkbashisms px script filename 在给定的
  • Express.js Passport认证自动失败跳过策略

    UPDATE 我已将代码从护照本地注册内部移至单独的处理程序 并且运行良好 问题在于 Passport 和本地注册的使用 但我不知道为什么 我使用 Node js Express Passport 进行身份验证和注册设置 这是以前使用过的标
  • 尝试在空对象引用上调用虚拟方法 -> 解析?

    我尝试使用一个按钮从一个主要活动切换到另一个活动 但是当我尝试运行该应用程序时 它在单击该应用程序后立即自行关闭 我在命令中发现错误消息 Unable to start activity ComponentInfo fr amseu mys