android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams


LinearLayout layout = (LinearLayout) mInflater.inflate(
                    R.layout.cell_check_item, null);
            LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                    UPDeviceInfo.getDeviceWidth(), LayoutParams.WRAP_CONTENT);
            rootLayout.addView(convertView, param);

我希望手動設置layout的寬度,結果就報錯(雖然很小的幾率報錯)

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

 

解決方法:換成從原來的view中獲取LayoutParams,而不是新建一個LayoutParams

LinearLayout.LayoutParams param = (android.widget.LinearLayout.LayoutParams) checkLayout
                    .getLayoutParams();
            param.width = UPDeviceInfo.getDeviceWidth();

看來,當一個View已經有了LayoutParams,最好不要添加一個新的LayoutParams。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM