2016-08-25 100 views
4

我將支持lib版本更新爲24.2.0,現在我的註冊屏幕已經死機。問題是在TextInputLayout,我有兩種方法:TextInputLayout setError方法在24.2.0中拋出ClassCastException

protected void setError(@Nullable CharSequence errorMsg, @NonNull EditText editText, boolean forceClean) { 
     TextInputLayout viewParent = (TextInputLayout) editText.getParent(); 
     if (forceClean) { 
      viewParent.setErrorEnabled(false); 
      viewParent.setError(null); 
     } 
     viewParent.setErrorEnabled(true); 
     viewParent.setError(errorMsg); 
    } 

    protected void clearError(@NonNull EditText editText) { 
     TextInputLayout viewParent = (TextInputLayout) editText.getParent(); 
     viewParent.setErrorEnabled(false); 
     viewParent.setError(null); 
    } 

我收到的時候我想的EditText的父轉換爲TextInputLayout,在佈局我有這樣的代碼是一個錯誤:

<android.support.design.widget.TextInputLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.design.widget.TextInputEditText 
      android:id="@+id/login_registration_firstname" 
      style="@style/registration_form_field" 
      android:hint="@string/login_registration_firstname" 
      android:inputType="textCapWords" /> 
</android.support.design.widget.TextInputLayout> 

所以,這個工作完全正常,但現在它拋出ClassCastException異常:

java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.support.design.widget.TextInputLayout 

我想知道是否有對一些新的指南?

回答

2

問題的調查表明,由於某些原因額外的佈局,所以現在我們有TextInputLayout - > FrameLayout裏 - > TextInputEditText那是可悲的:(以便臨時解決方法我已經創建了下面的方法:

@Nullable 
private TextInputLayout getTextInputLayout(@NonNull EditText editText) { 
     View currentView = editText; 
     for (int i = 0; i < 2; i++) { 
      ViewParent parent = currentView.getParent(); 
      if (parent instanceof TextInputLayout) { 
       return (TextInputLayout) parent; 
      } else { 
       currentView = (View) parent; 
      } 
     } 
     return null; 
} 

這人會發現你的TextInputLayout在視圖層次

如果你是幸運的,你會發現這個錯誤的粉紅色的顏色變化,簡單的方法來克服它覆蓋樣式,在styles.xml:

<style name="TextAppearance.Design.Error" parent="TextAppearance.AppCompat.Caption" tools:override="true"> 
     <item name="android:textColor">@color/red</item> 
</style> 
+1

我想知道,爲什麼像這樣簡單的事情需要解決方法? – Shaishav

+0

@ Shaishav我認爲這是一種反模式 - 他們總是說層次結構應該是平坦的,而且這個層次增加了兩個額外的層次,最開始EditText負責這個 - 只有一個視圖。 –

+0

我不認爲這可能是一個好的解決方案。 Android支持庫團隊可以隨時更改它,並且您還可以使用循環在視圖層次結構中導航,並測試每個視圖是否爲實例。只需使用InputTextLayout作爲參數即可。 –

1

我遇到同樣的問題,我最終使用該庫 - >MaterialEditTest誰更可靠,並提供更多的功能。

有了那一個,你不需要嵌套的元素,你可以修改小標籤的顏色和錯誤信息......

+0

我並不認爲這是問題的解決方案,但似乎我最終會得到lib,設計Lib開發人員改變他們的想法太快:) upvoted! –

+0

但是,您可以在本地添加庫以避免對它們進行任何進一步的修改。或者只是指定要使用的庫的版本* static * – Jaythaking

0

您可以檢查TextInputLayout v24.2.x.的代碼
現在它可以與FrameLayout一起使用。

public TextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
    // Can't call through to super(Context, AttributeSet, int) since it doesn't exist on API 10 
    super(context, attrs); 
    //... 
    mInputFrame = new FrameLayout(context); 
    mInputFrame.setAddStatesFromChildren(true); 
    addView(mInputFrame); 

    //.... 

} 

@Override 
public void addView(View child, int index, final ViewGroup.LayoutParams params) { 
    if (child instanceof EditText) { 
     mInputFrame.addView(child, new FrameLayout.LayoutParams(params)); 
     //... 
    } else { 
     // Carry on adding the View... 
     super.addView(child, index, params); 
    } 
} 

其中mInputFrameFrameLayout

這是您的問題(父母是FrameLayout)的原因。

java.lang.ClassCastException:android.widget.FrameLayout不能轉換到android.support.design.widget.TextInputLayout

您應該使用TextInputLayout作爲參數,而不是在視圖層次導航。

+0

您可能沒有讀過該問題,因爲您不瞭解該問題。當然,你可以通過id找到TextInputLayout並在那裏設置錯誤,但重點是你有EditText,你需要設置Error。 –

+0

@ViktorYakunin您不需要EditText來設置錯誤。你只需要InputTexLayout。您只使用editText獲取父視圖。只是使用保護無效clearError(@NonNull InputTextLayout){...} –

+0

的方法也許我的英文不好,我沒有TextInputLayout的id,只有EditText的引用。如果你通過代碼,你會發現我已經在使用TextInputLayout的方法。 –

相關問題