AppCompatEditText.getpParent () in TextInputLayout gibt FrameLayout @ zurüc

Ich erstelle einen einfachen AppCompatEditText, füge OnFocusChangeListener hinzu und füge ihn in das einfache TextInputLayout ein.

Wenn AppCompatEditText den Fokus verliert, sollte der Inhalt mit der Methode isValidParam überprüft werden.

Es hat bis gestern funktioniert, als ich Rev.23.0.3 verwendet habe. Aber jetzt, als ich Rev.24.0.2 verwendet habe, gibt es in der ersten Zeile der isValidParam-Methode den folgenden Fehler.

java.lang.ClassCastException: android.widget.FrameLayout kann nicht in android.support.design.widget.TextInputLayout @ umgewandelt werd

Ich habe im Debugging-Modus geprüft. AppCompatEditText.getpParent () gibt statt TextInputLayout wirklich Framelayout zurück.

LinearLayout llParams = new LinearLayout(context);
llParams.setOrientation(LinearLayout.VERTICAL);

// Create label for param
final TextInputLayout tilParam = new TextInputLayout(context);
// Add label into layout
llParams.addView(tilParam);


// Create Editor for param
final AppCompatEditText etParam = new AppCompatEditText(context);

edParam.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus)
            if (isValidParam(etParam)) {
                do some thing;
            } else {
                do other thing;
            }
    }
});

tilParam.addView(etParam);


// validation method
boolean isValidParam(AppCompatEditText editText) {
    TextInputLayout til = (TextInputLayout) editText.getParent();

    String text = editText.getText().toString().trim();

    if (!text.equls("some criteria") {
        till.setError("Error text")
        return false;
    }

    return true;
}

Antworten auf die Frage(10)

Ihre Antwort auf die Frage