You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发求助:点击按钮为何无法启动新Activity?

Android Activity跳转失效的排查及解决

可能的问题点及解决方法:

  • 变量名冲突:你把Intent类名当作变量名使用了,Kotlin中类名首字母大写,变量名需遵循小驼峰命名规则,这会导致编译器混淆。修改代码如下:

    val button3 = findViewById<Button>(R.id.button3)
    button3.setOnClickListener {
        val intent = Intent(this, muse_pick_duration::class.java) // 将Intent改为小写开头的intent
        startActivity(intent)
    }
    
  • 目标Activity未正确注册:务必确认muse_pick_duration已在AndroidManifest.xml的<application>标签内完成注册,正确格式示例:

    <activity android:name=".muse_pick_duration"/>
    

    若Activity位于子包下,需填写完整类路径(例如com.your.app.subpackage.muse_pick_duration)。

  • 按钮ID不匹配:检查布局文件中按钮的ID是否确实为button3,避免拼写错误导致findViewById获取不到正确的按钮实例。

  • 上下文(Context)错误:如果这段代码写在Activity的内部类中,要确认this指向的是当前muse_home实例;如果是在Fragment中,需将this替换为requireContext()或activity(注意判空)。

内容的提问来源于stack exchange,提问作者Nayana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 05:05:16