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

Android Studio中Activity切换导致应用崩溃的问题排查

问题描述

我正在开发首个交互式Android应用,尝试在Android Studio中实现Activity间导航。但使用切换Activity的代码时应用会崩溃,移除该导航代码后应用无异常。

DashboardActivity代码

package com.example.shashank.login;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class DashboardActivity extends AppCompatActivity {

    String EmailHolder;
    TextView Email;
    Button Next;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);

        Email = findViewById(R.id.textView1);
        Next = findViewById(R.id.button1);

        Intent intent = getIntent();

        // Receiving User Email Send By MainActivity.
        EmailHolder = intent.getStringExtra(MainActivity.UserEmail);

        // Setting up received email to TextView.
        Email.setText(Email.getText().toString() + EmailHolder);


    }


    public void nextActivity(View view) {
        Intent intent = new Intent(DashboardActivity.this, userdashboard.class);
        startActivity(intent);
    }
}

DashboardActivity对应的XML布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_dashboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.shashank.login.DashboardActivity">


    <TextView
        android:text="@string/login_successful"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="#000"
        android:id="@+id/textView1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="176dp" />

    <Button
        android:text="@string/next"
        android:id ="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:onClick="nextActivity"
        android:layout_marginTop="36dp"
        />
</RelativeLayout>

我希望从DashboardActivity跳转到userdashboard类,但点击按钮后应用崩溃。请问代码存在什么问题?如何实现无崩溃的Activity导航?


问题分析与解决方案

核心崩溃原因

你的代码最可能的崩溃诱因是未在AndroidManifest.xml中注册userdashboard Activity。Android系统要求所有需要启动的Activity必须在清单文件中声明,否则启动时会抛出ActivityNotFoundException直接导致应用崩溃。

另外还有一个不符合编码规范的点:Java类名遵循大驼峰命名规则,userdashboard应改为UserDashboard,这虽不会直接引发崩溃,但长期来看易导致代码维护问题。

修复步骤

  1. 注册目标Activity到AndroidManifest.xml
    打开app/src/main/AndroidManifest.xml,在<application>标签内添加userdashboard的声明:
<activity
    android:name=".userdashboard"
    android:exported="false" />

若后续无需外部应用启动该Activity,保持exported="false"更安全;反之可设为true。

  1. (推荐)修正类名规范
    将userdashboard.java文件名改为UserDashboard.java,同时更新类定义与跳转代码:
  • 类定义:
public class UserDashboard extends AppCompatActivity {
    // 你的Activity逻辑代码
}
  • DashboardActivity中的跳转代码:
public void nextActivity(View view) {
    Intent intent = new Intent(DashboardActivity.this, UserDashboard.class);
    startActivity(intent);
}

同步更新AndroidManifest.xml中的Activity名称:

<activity
    android:name=".UserDashboard"
    android:exported="false" />
  1. 额外排查项
    若注册后仍崩溃,检查以下内容:
  • userdashboard(或UserDashboard)是否正确继承自AppCompatActivity或Activity
  • 目标Activity对应的布局文件是否存在且无编译错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 08:10:42