gpt4 book ai didi

java - 如何解决我制作的 AlertDialog 上的错误 androidx.appcompat.widget.AppCompatButton 无法抛出到 android.widget.ImageView

转载 作者:行者123 更新时间:2023-12-01 17:58:10 27 4
gpt4 key购买 nike

我尝试制作AlertDialog,但遇到崩溃错误问题,尝试了多种方法但仍然不起作用,从通过添加Id将constrainlayout更改为LinearLayout但此方法仍然有效

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>

我尝试过这个方法how to fix androidx.appcompat.widget.AppCompatButton cannot be cast to com.rey.material.widget.Button通过添加 id 并将其更改为 Linearlayout 但它仍然崩溃

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()
}

最佳答案

要显示弹出窗口...请按照以下代码操作,并根据您的要求在 xml 中进行必要的设计。

 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()

}

}

使用 inflater 来获取您最初提到的 xml 文件

关于java - 如何解决我制作的 AlertDialog 上的错误 androidx.appcompat.widget.AppCompatButton 无法抛出到 android.widget.ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60680171/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com