-3

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>
sirEgghead
  • 236
  • 1
  • 12
  • 2
    You're presumably getting a `ClassCastException` because `SignInButton` is not a `Button`. Change your `btnSignIn` to a `SignInButton` instead of just plain `Button`. Also, for any kind of crash, the first thing to look at is the stack trace, which you need to post with questions like this: https://stackoverflow.com/q/23353173. – Mike M. May 05 '22 at 01:57
  • Mike, thank you. I will look up about the stack trace. Also, the button was added via by following a tutorial that used the same button as me. However, I would say that I should still be able to find the id of a `SignInButton` element anyway, correct? – sirEgghead May 05 '22 at 01:59
  • 2
    It's nothing to do with finding it, or the ID. You've declared the wrong type in the `Activity`. Change to `private lateinit var btnSignIn: SignInButton`. – Mike M. May 05 '22 at 02:00
  • 1
    Ah, good catch. Thank you. I would like to take a few moments to dig into this. I appreciate it. – sirEgghead May 05 '22 at 02:01
  • 1
    Mike, that was it. Silly oversight. Thanks a ton. Also, I will keep in mind about adding a stack trace to the question in the future. I'm not overly familiar with Android Studio, but I will learn it. :) – sirEgghead May 05 '22 at 02:03
  • 1
    No problem. I should mention, for many cases that would've been just fine, because most of the time, `View`s with `Button` in the name are actually descendants of the `Button` class; e.g., `AppCompatButton`, `MaterialButton`, etc. In this case, though, [`SignInButton`](https://developers.google.com/android/reference/com/google/android/gms/common/SignInButton) is actually a `FrameLayout`; a wholly custom button. Just FYI. Anyhoo, glad you got it working. Cheers! – Mike M. May 05 '22 at 02:10

2 Answers2

1

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!

sirEgghead
  • 236
  • 1
  • 12
0

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

ruby6221
  • 228
  • 1
  • 7