Home > Software engineering >  findViewById Crashing App In New Activity
findViewById Crashing App In New Activity

Time:05-05

I have searched through multiple similar questions on SO and have not found a solution that is working for me. I created a blank activity and have the following code in it. When btnSignIn = findViewById(R.id.btnSignIn) is commented out, the app runs fine. Below is the code in my activity.

class LoginActivity : AppCompatActivity() {
    private lateinit var btnSignIn: Button


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        btnSignIn = findViewById(R.id.btnSignIn) 
    }
}

Not sure if it matters or not, but LoginActivity is a blank activity that I created in addition to the MainActivity that was created upon project creation. I also set the LoginActivity to load first with the app by changing my manifest.

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.WaOCompanion">
    <activity
        android:name=".LoginActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:exported="true">
    </activity>
</application>

Update: added activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".LoginActivity">

    <TextView
        android:id="@ id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="140dp"
        android:fontFamily="sans-serif-medium"
        android:text="WaO Companion"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.gms.common.SignInButton
        android:id="@ id/btnSignIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

Apparently you use a wrong type. the view type is SignInButton rather than Button. Change it to
private lateinit var btnSignIn: SignInButton

CodePudding user response:

User Mike M. caught the problem in the comments. The solution was to change my private lateinit var btnSignIn: Button to private lateinit var: SignInButton. Thanks Mike!

CodePudding user response:

Check your activity_login layout properly. I think there is no Button element whose id is btnSignIn.

  • Related