木槿74138 2021-12-23 11:56 采纳率: 88.9%
浏览 135
已结题

Android,button按钮按下后APP自动闪退,求解,如何解决?

上午使用Button组件时用到了点击事件,自己写了一个组件“btn4”运行之后显示正常,但是点击之后APP会自己闪退,有没有可以帮忙解决一下的,谢谢。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:ignore="OnClick"
    android:orientation="vertical">


    <Button
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/btn_1"
        android:text="按钮"
        android:textColor="#0066FF"
        android:textSize="30sp"
        android:background="#FF0000"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/btn_2"
        android:text="按钮1"
        android:textColor="#0066FF"
        android:textSize="30sp"
        android:background="@drawable/bg_btn2"
        android:layout_below="@id/btn_1"
        android:layout_marginTop="10dp"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/btn_3"
        android:text="按钮2"
        android:textColor="#0066FF"
        android:textSize="30sp"
        android:background="@drawable/bg_btn3"
        android:layout_below="@id/btn_2"
        android:layout_marginTop="10dp"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/btn_4"
        android:text="按钮3"
        android:textColor="#0066FF"
        android:textSize="30sp"
        android:background="@drawable/bg_btn4"
        android:layout_below="@id/btn_3"
        android:layout_marginTop="10dp"
        android:onClick="showToast"
        />

</RelativeLayout>

package com.example.text5;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class ButtonActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
        public void showToast(View view){
        Toast.makeText(this, "我被点击了", Toast.LENGTH_SHORT).show();
    }
}

img

  • 写回答

5条回答 默认 最新

  • JMchen123 2021-12-24 13:59
    关注

    1、我盲猜是你的ButtonActivity还没注册到AndroidManifest中
    2、你的ButtonActivity没有相对应的xml布局文件,activity_main这个布局文件只是你的MainActivity的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 1月15日
  • 已采纳回答 1月7日
  • 创建了问题 12月23日

悬赏问题

  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?