为什么在Edittext

时间:2019-12-12 02:06:14

标签: android android-alertdialog android-cardview

您好,有以下例外情况,请提供帮助。

2019-12-11 20:32:06.157 24492-24492/com.example.mac.mycarapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mac.mycarapp, PID: 24492
java.lang.ClassCastException: androidx.cardview.widget.CardView cannot be cast to android.widget.EditText
    at com.example.mac.mycarapp.UI.EPayco$6.onClick(EPayco.java:324)
    at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

这是xml代码...

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="70dp"
    >

    <ImageView
        android:layout_width="110dp"
        android:layout_marginTop="70dp"
        android:layout_marginLeft="30dp"
        android:layout_height="50dp"
        android:src="@drawable/lifeon_logo"
        android:scaleType="fitXY"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fontFamily="@font/catamaran_extrabold"
    android:textColor="@android:color/black"
    android:text="Porfavor ingresa el pin \n Recibido"
    android:layout_marginTop="20dp"
    android:textSize="22sp"
    android:gravity="center_horizontal"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:orientation="horizontal"
        android:padding="20dp"
        android:layout_marginLeft="62dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="120dp">

        <androidx.cardview.widget.CardView
            android:layout_width="43dp"
            android:layout_height="60dp"
            app:cardCornerRadius="10dp"
            android:elevation="11dp"
            >

            <EditText
                android:id="@+id/pin1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fontFamily="@font/catamaran_extrabold"
                android:hint="  5"
                android:textColor="@android:color/black"
                android:textSize="25sp" />
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="43dp"
            android:layout_height="60dp"
            android:elevation="11dp"
            android:layout_marginLeft="20dp"
            app:cardCornerRadius="10dp">

            <EditText
                android:id="@+id/pin2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fontFamily="@font/catamaran_extrabold"
                android:hint="  5"
                android:textColor="@android:color/black"
                android:textSize="25sp" />
        </androidx.cardview.widget.CardView>
        <androidx.cardview.widget.CardView
            android:layout_width="43dp"
            android:layout_height="60dp"
            android:elevation="11dp"
            android:layout_marginLeft="20dp"
            app:cardCornerRadius="10dp">

            <EditText
                android:id="@+id/pin3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fontFamily="@font/catamaran_extrabold"
                android:hint="  5"
                android:textColor="@android:color/black"
                android:textSize="25sp" />
        </androidx.cardview.widget.CardView>
        <androidx.cardview.widget.CardView
            android:id="@+id/pin4"
            android:layout_width="43dp"
            android:layout_height="60dp"
            android:elevation="11dp"
            android:layout_marginLeft="20dp"
            app:cardCornerRadius="10dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fontFamily="@font/catamaran_extrabold"
                android:hint="  5"
                android:textColor="@android:color/black"
                android:textSize="25sp" />
        </androidx.cardview.widget.CardView>


    </LinearLayout>
</LinearLayout>

这是警报对话框代码。

public void showAlertDialogButtonClicked(View view,int random) {
    // create an alert builder
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("");
    // set the custom layout
    final View customLayout = LayoutInflater.from(this).inflate(R.layout.validation_alert_dialog_custom_layout, null);
    builder.setView(customLayout);
    // add a button
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // send data from the AlertDialog to the Activity
            EditText editText = customLayout.findViewById(R.id.pin1);
            EditText editText2 = customLayout.findViewById(R.id.pin2);
            EditText editText3 = customLayout.findViewById(R.id.pin3);
            EditText editText4 = customLayout.findViewById(R.id.pin4);

            String num1=editText.getText().toString();
            String num2=editText2.getText().toString();
            String num3=editText3.getText().toString();
            String num4=editText4.getText().toString();
            //sendDialogDataToActivity(num1,num2,num3,num4);

            String hole=num1+num2+num3+num4;
            int holeInt=Integer.parseInt(hole);
            if(random==holeInt){

            }
        }
    });
    // create and show the alert dialog
    AlertDialog dialog = builder.create();
    dialog.show();
}

这个问题根源是我可能无法在警报对话框内进行名片视图或在警报对话框内无法编辑文本。我认为我不需要在警报对话框内进行名片视图的findviewById调用。

0 个答案:

没有答案