-3

Edit: I found the solution, for some reason the code does not run android studio if it is linked to the GitHub repository. I pushed the same version to a new branch on GitHub and downloaded the code as a zip file, opened it in android studio and it's working.

Code from XML for LoginActivity

<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"
    android:background="@drawable/gradient_color_2"
    tools:context=".LoginActivity">

    <EditText
        android:id="@+id/username_loginActivity_txt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="80dp"
        android:layout_marginTop="310dp"
        android:layout_marginEnd="80dp"
        android:background="@drawable/rounded_corner"
        android:ems="10"
        android:gravity="center"
        android:hint="@string/email_login"
        android:inputType="textEmailAddress"
        android:textColor="#FFFFFF"
        android:textColorHint="#BDBDBD"
        android:textSize="13sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:importantForAutofill="no" />

    <EditText
        android:id="@+id/password_loginActivity_txt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:background="@drawable/rounded_corner"
        android:ems="10"
        android:inputType="textPassword"
        android:gravity="center"
        android:hint="@string/password_login"
        android:textColorHint="#BDBDBD"
        android:textColor="#FFFFFF"
        android:textSize="13sp"
        app:layout_constraintEnd_toEndOf="@+id/username_loginActivity_txt"
        app:layout_constraintStart_toStartOf="@+id/username_loginActivity_txt"
        app:layout_constraintTop_toBottomOf="@+id/username_loginActivity_txt" />

    <Button
        android:id="@+id/button_loginActivity_btt"
        android:layout_width="0dp"
        android:layout_height="44dp"
        android:layout_marginTop="33dp"
        android:background="@drawable/rounded_corner_buttons"
        android:text="@string/login"
        android:textColor="@color/black"
        app:layout_constraintEnd_toEndOf="@+id/password_loginActivity_txt"
        app:layout_constraintStart_toStartOf="@+id/password_loginActivity_txt"
        app:layout_constraintTop_toBottomOf="@+id/password_loginActivity_txt" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="147dp"
        android:layout_marginBottom="35dp"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="@+id/username_loginActivity_txt"
        app:layout_constraintEnd_toEndOf="@+id/username_loginActivity_txt"
        app:layout_constraintHorizontal_bias="0.495"
        app:layout_constraintStart_toStartOf="@+id/username_loginActivity_txt"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        android:id="@+id/dont_have_account_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="49dp"
        android:text="@string/i_do_not_have_an_account"
        android:textColor="#9E9E9E"
        app:layout_constraintEnd_toEndOf="@+id/button_loginActivity_btt"
        app:layout_constraintStart_toStartOf="@+id/button_loginActivity_btt"
        app:layout_constraintTop_toBottomOf="@+id/button_loginActivity_btt" />

</androidx.constraintlayout.widget.ConstraintLayout>

If I try to add any new element to the XML I will get NullPointException error.

button_loginActivity_btt.setOnClickListener {
            userLogin()
        }

The function called on the setOnClickListener

private fun userLogin() : Unit {
        val email = username_loginActivity_txt.text.toString()
        val password = password_loginActivity_txt.text.toString()

        if (email.isEmpty() || password.isEmpty()) {
            Toast.makeText(this, "Please provide a e-mail and password", Toast.LENGTH_SHORT).show()
            return
        }

        FirebaseAuth.getInstance().signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(this) {
                if (it.isSuccessful) {
                    Log.d("Login", "Successfully logged in: ${it.result.user!!.uid}")
                    startActivity(Intent(this, AppActivity::class.java))
                }
                else {
                    return@addOnCompleteListener
                }
            }
            .addOnFailureListener {
                Log.d("Login", "Failed to log in: ${it.message}")

                if (it.message == "There is no user record corresponding to this identifier. The user may have been deleted.") {
                    Toast.makeText(this, "E-mail or password is wrong", Toast.LENGTH_SHORT).show()
                }
                else {
                    Toast.makeText(this, "Invalid e-mail", Toast.LENGTH_SHORT).show()
                }
            }
    }

Android Studio message error. I tried to debug the button_loginActivity_btt.setOnClickListerner and the savedInstanceState is null.

Process: com.example.chatapp, PID: 4195
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chatapp/com.example.chatapp.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.chatapp.LoginActivity.onCreate(LoginActivity.kt:21)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
  • what do you mean with `If I remove the new Activity`? – kingston Mar 03 '22 at 20:32
  • If I remove the Activity I do not get the error, but I get the same error If I add any element to login XML. I can't add any new activity or textview or button – Mateus Jorge Mar 03 '22 at 22:37
  • Any new element* – Mateus Jorge Mar 03 '22 at 22:42
  • Found the solution, is something related to the code linked to my github. I have uploaded the code to a new branch and downloaded it, opened in android studio and is running fine! Sorry for my bad english, I will try to fix it later. – Mateus Jorge Mar 03 '22 at 23:25

1 Answers1

0

I found the solution, for some reason the code does not run android studio if it is linked to the GitHub repository. I pushed the same version to a new branch on GitHub and downloaded the code as a zip file, opened it in android studio and it's working.