Home > Software engineering >  How to connect two activities in Android Studio?
How to connect two activities in Android Studio?

Time:01-14

I am trying to create a lonin page where you enter your email address and password. If they are correct you will be taken to another page (activity) but my app keeps crashing and I have tried everything but still stuck here.

My code is below, thank you in advance.


class MainActivity : AppCompatActivity() {

    @SuppressLint("MissingInflatedId", "WrongViewCast")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val btn = findViewById<Button>(R.id.btn1)
        val email = findViewById<TextView>(R.id.email)
        val password = findViewById<TextView>(R.id.password)


        fun onClick(view: View) {
            if (email.toString() == "safin" || password.toString() == "hallo") {
                btn.setOnClickListener {
                    startActivity(Intent(this, MainActivity2::class.java))
                }
            } else {
                email.error = "Your Email and your Password did not match!"
                password.error = "Your Password and your Email did not match!"
            }

        }

    }



CodePudding user response:

This is my xml code: 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="10dp"
    >


    <EditText
        android:id="@ id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:width="300dp"
        android:hint="@string/email"
        android:layout_margin="15dp"
        android:inputType="textEmailAddress"
        tools:layout_editor_absoluteX="95dp"
        tools:layout_editor_absoluteY="316dp" />

    <EditText
        android:id="@ id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/password"
        android:layout_margin="10dp"
        android:width="300dp"
        android:inputType="textPassword"
        tools:layout_editor_absoluteX="98dp"
        tools:layout_editor_absoluteY="389dp" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@ id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="200dp"
        android:layout_margin="15dp"
        android:text="@string/click_me"
        android:onClick="onClick"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.696" />


</LinearLayout>

CodePudding user response:

did you add activity in android manifest ?

  • Related