无法在 android studio 中的活动之间切换

Not able to switch between activities in android studio

提问人:acorn 提问时间:6/9/2023 最后编辑:acorn 更新时间:7/7/2023 访问量:35

问:

我正在构建我的第一个交互式 Android 应用程序,并尝试找到一种在 Android Studio 中的活动之间导航的方法。使用用于切换活动的代码时,应用将关闭。删除用于导航到其他活动的代码时,应用不会关闭/出现 bug。

这是我正在使用的代码:

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);
    }
}

这是仪表板活动的 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。它应从 DashboardActivity 导航到 userdashboard 类,但应用会关闭。

这段代码有什么问题?如何在不关闭应用程序的情况下在活动之间导航?

java 按钮 android-activity 切换

评论

0赞 user2342558 6/9/2023
您在 Android Studio 的 logcat/run 面板中是否有任何错误/警告?如果生成失败,应首先解决所有问题。

答:

2赞 Khush Parmar 7/7/2023 #1

如果要在两个活动之间导航,则可以使用以下代码。

public class DashboardActivity extends AppCompatActivity {

TextView Email;
Button Next;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Email = findViewById(R.id.textView1);
    Next = findViewById(R.id.button1);
    
    // Here i have created click listener so when you tap on the next button that will invoke this click listener and inside it we are calling the nextActivity function to open new activity.
    Next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            nextActivity();
        }
    });
}

public void nextActivity() {
    // Here i passed two activity first is the current activity and second is the name of the activity which you want to navigate from the current activity.
    Intent intent = new Intent(DashboardActivity.this, NextActivity.class);
    startActivity(intent);
}

}

您可以使用此代码放置按钮单击侦听器来调用从一个活动到另一个活动的导航。