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

Android Studio中FirstActivity按钮不显示问题求助

问题:跳转至FirstActivity后按钮等控件不显示

我是一名大学生,正在用Android Studio完成课程项目,之前一切正常,但现在跳转至FirstActivity后,模拟器和真机上都看不到按钮等控件。已经试过重启软件、清除缓存、同步Gradle,问题还是没解决。

相关代码如下:

FirstActivity布局XML

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="158dp"
        android:layout_marginTop="185dp"
        android:text="Button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

FirstActivity代码(存在问题版本)

package com.example.blatt4.Aufgabe1

import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.MotionEvent
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.example.blatt4.MainActivity
import com.example.blatt4.R

class FirstActivity:AppCompatActivity() {
    val TAG = "FirstActivity"

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        setContentView(R.layout.activity_first)

        val button = findViewById<Button>(R.id.button2)

        button.setOnClickListener{
            startActivity(Intent(this, MainActivity::class.java))
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event!!.actionMasked) {
            MotionEvent.ACTION_DOWN -> Log.d(TAG, "Touch event started at x = " + event.x + ", y = " + event.y)
            MotionEvent.ACTION_UP -> Log.d(TAG, "User lifted their finger at x = " + event.x + ", y = " + event.y);
            MotionEvent.ACTION_MOVE -> Log.d(TAG, "User is moving their finger at x = " + event.x + ", y = " + event.y)

            MotionEvent.ACTION_CANCEL -> Log.d(TAG, "User canceled the touch event at x = " + event.x + ", y = " + event.y);
            MotionEvent.ACTION_BUTTON_PRESS -> Log.d(TAG, "Button pressed at x = " + event.x + ", y = " + event.y);

            //Multi Touch
            /**
            MotionEvent.ACTION_POINTER_DOWN -> Log.d(TAG, "Pointer Down" + event.x + ", y=" + event.y);
            MotionEvent.ACTION_POINTER_UP -> Log.d(TAG, "Pointer Up" + event.x + ", y=" + event.y);
            */
        }
        return true
    }
}

正常显示的MainActivity代码

package com.example.blatt4

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.example.blatt4.Aufgabe1.FirstActivity
import com.example.blatt4.Aufgabe2.SecondActivity
import com.example.blatt4.Aufgabe3.ThirdActivity
import com.example.blatt4.Aufgabe4.FourthActivity

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

        val aufgabe1 = findViewById<Button>(R.id.aufgabe1)
        val aufgabe2 = findViewById<Button>(R.id.aufgabe2)
        val aufgabe3 = findViewById<Button>(R.id.aufgabe3)
        val aufgabe4 = findViewById<Button>(R.id.aufgabe4)

        aufgabe1.setOnClickListener {
            startActivity(Intent(this, FirstActivity::class.java))
        }
        aufgabe2.setOnClickListener {
            startActivity(Intent(this, SecondActivity::class.java))
        }
        aufgabe3.setOnClickListener {
            startActivity(Intent(this, ThirdActivity::class.java))
        }
        aufgabe4.setOnClickListener {
            startActivity(Intent(this, FourthActivity::class.java))
        }
    }
}

MainActivity布局XML

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/aufgabe1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="150dp"
        android:layout_marginTop="170dp"
        android:text="Aufgabe1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/aufgabe2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="150dp"
        android:layout_marginTop="53dp"
        android:text="Aufgabe2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/aufgabe1" />

    <Button
        android:id="@+id/aufgabe3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="150dp"
        android:layout_marginTop="53dp"
        android:text="Aufgabe3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/aufgabe2" />

    <Button
        android:id="@+id/aufgabe4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="150dp"
        android:layout_marginTop="53dp"
        android:text="Aufgabe4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/aufgabe3" />

</androidx.constraintlayout.widget.ConstraintLayout>

问题根源

FirstActivity重写了错误的onCreate方法签名——带PersistableBundle参数的重载方法是用于特殊持久化场景的,默认情况下系统不会触发这个方法,导致setContentView(R.layout.activity_first)从未执行,布局根本没加载,控件自然无法显示。

解决方案

修改FirstActivity的onCreate方法,使用标准的参数签名(仅接收Bundle?类型参数):

修正后的FirstActivity代码

package com.example.blatt4.Aufgabe1

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.example.blatt4.MainActivity
import com.example.blatt4.R

class FirstActivity:AppCompatActivity() {
    val TAG = "FirstActivity"

    // 修正onCreate方法签名
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_first)

        val button = findViewById<Button>(R.id.button2)

        button.setOnClickListener{
            startActivity(Intent(this, MainActivity::class.java))
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event!!.actionMasked) {
            MotionEvent.ACTION_DOWN -> Log.d(TAG, "Touch event started at x = " + event.x + ", y = " + event.y)
            MotionEvent.ACTION_UP -> Log.d(TAG, "User lifted their finger at x = " + event.x + ", y = " + event.y);
            MotionEvent.ACTION_MOVE -> Log.d(TAG, "User is moving their finger at x = " + event.x + ", y = " + event.y)

            MotionEvent.ACTION_CANCEL -> Log.d(TAG, "User canceled the touch event at x = " + event.x + ", y = " + event.y);
            MotionEvent.ACTION_BUTTON_PRESS -> Log.d(TAG, "Button pressed at x = " + event.x + ", y = " + event.y);

            //Multi Touch
            /**
            MotionEvent.ACTION_POINTER_DOWN -> Log.d(TAG, "Pointer Down" + event.x + ", y=" + event.y);
            MotionEvent.ACTION_POINTER_UP -> Log.d(TAG, "Pointer Up" + event.x + ", y=" + event.y);
            */
        }
        return true
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 16:53:10