2015-05-05 53 views
0

我試圖從文本更改偵聽器隱藏工具欄錯誤與程序兼容性-V7隱藏工具欄時

public void onTextChanged(CharSequence s, int start, int before, int count) { 
    toolbar.animate() 
     .translationY(-toolbar.getBottom()) 
     .setInterpolator(new AccelerateInterpolator()) 
     .start(); 
} 

該解決方案完美地工作在更高級別的API(上奇巧成功運行),但我得到了下面就錯誤與API級別運行裝置10

java.lang.NoSuchMethodError: android.support.v7.widget.Toolbar.animate

佈局XML部分是

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:background="#ff6d7fe2" 
    app:contentInsetEnd="0dp"> 
</android.support.v7.widget.Toolbar> 

我沒有設置工具欄作爲支持操作bar.Why即使使用android.support.v7.widget.Toolbar,我得到這個錯誤?

回答

1

此錯誤是因爲您正在使用ViewPropertyAnimator方法toolbar.animate()。而這僅僅是與API級以上12所以,你可以使用JakeWharton的NineOldAndroid庫提與API的比12

+0

我的項目已經包含了用於滑動drawer的nineoldandroids-2.4.0.jar。是否在上面的情況下生效了相同的lib? – Nidhin

+0

檢查班級中的進口。是從nineoldandroid還是從android的 – Sajal

+0

請確保您使用庫中的toolbar.animate(),而不是native –

1

Toolbar下它兼容不兼容時,只是一個視圖,animate是V10後推出的View的方法 - 因此,當您嘗試在v10設備上調用它時,它不存在並崩潰。

您可以使用

ViewCompat.animate(toolbar).translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start(); 

但是這不會做任何動畫預ICS。您可以像Sajal建議的那樣使用NineOldAndroid,將動畫放在較舊的設備上,或者使用舊的動畫框架爲視圖設置動畫。

相關問題