简体   繁体   中英

how to resolve mistakes androidx.appcompat.widget.AppCompatButton cannot be thrown to android.widget.ImageView on the AlertDialog that I made

I tried to make AlertDialog but I got a crash error problem, I tried a number of ways but it still didn't work, from changing the constrainlayout to LinearLayout by adding Id but this method still works

xml

<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:id="@+id/parent_view"
android:padding="20dp">

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardCornerRadius="5dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="16dp">

        <TextView
            android:id="@+id/content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="Cencel payment"
            android:textAllCaps="true"
            android:textColor="@color/color_black_000000"
            app:layout_constraintEnd_toEndOf="@+id/textView10"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="@+id/textView10"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="Confirm if you are sure to cancel this payment."
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/content" />

        <Button
            android:id="@+id/btnContonuewPayment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="20sp"
            android:layout_marginTop="23dp"
            android:layout_marginEnd="20sp"
            android:background="@drawable/bg_payment_orange_ff8830"
            android:text="Continue Payment"
            android:textColor="@color/color_white_FFFFFF"
            app:layout_constraintEnd_toEndOf="@+id/textView10"
            app:layout_constraintStart_toStartOf="@+id/textView10"
            app:layout_constraintTop_toBottomOf="@+id/textView10" />

        <Button
            android:id="@+id/btnCancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="20sp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="20sp"
            android:background="@drawable/bg_back_to_home"
            android:text="Yes, Cancel"
            android:textAllCaps="false"
            android:textColor="#FF8830"
            app:layout_constraintEnd_toEndOf="@+id/btnContonuewPayment"
            app:layout_constraintStart_toStartOf="@+id/btnContonuewPayment"
            app:layout_constraintTop_toBottomOf="@+id/btnContonuewPayment" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

I tried this method how to fix androidx.appcompat.widget.AppCompatButton cannot be cast to com.rey.material.widget.Button by adding id and changing it to Linearlayout but it still crashes

kotlin

private fun showDialog() {

    val dialogBuilder = AlertDialog.Builder(this)
    val viewDialogForget = layoutInflater.inflate(R.layout.dialog_payment,null)
    val continuew = viewDialogForget.findViewById<ImageView>(R.id.btnContonuewPayment)
    val cancel = viewDialogForget.findViewById<ImageView>(R.id.btnCancel)
    dialogBuilder.setView(viewDialogForget)
    dialogBuilder.setCancelable(false)
    val dialog = dialogBuilder.create()
    dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))


    continuew.setOnClickListener {
        dialog.dismiss()
    }

    cancel.setOnClickListener {
        finish()
    }

    dialog.show()
}

No need to create additional buttons in xml for alert dialog it can be done smoothly by coding. Here is an example of it.

private fun setupAlertDialogButton() {
    alertDialogButton.setOnClickListener {
        MaterialAlertDialogBuilder(this)
            .setTitle("Discard draft?")
            .setMessage("Your message.")
            .setPositiveButton("Discard") { dialog, which ->
                Toast.makeText(this, "Clicked discard", Toast.LENGTH_SHORT).show()
            }
            .setNegativeButton("Cancel") { dialog, which ->
                Toast.makeText(this, "Clicked cancel", Toast.LENGTH_SHORT).show()
            }
            .show()
    }
}

alertDialogButton should be your backbutton ID

To display like pop up ...follow below code and make necessary design in xml as per ur requirement.

 class Testing: AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.testing)


    imageButton.setOnClickListener {

        setupAlertDialog()
    }

}

private fun setupAlertDialog() {

var dialog = AlertDialog.Builder(this)

   var inflater = this.layoutInflater

    var message:View = inflater.inflate(R.layout.test_dialog,null)

    dialog.setView(message)
    dialog.show()

}

}

use inflater to pur your xml file the one you mentioned initially

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM