java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widg
有時候需要在在程式碼中設定LayoutParams,自己為一個FrameLayout設定LayoutParams的時候,遇上如題問題,
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
FrameLayout的父控制元件是一個LinearLayout控制元件,問題出在,LinearLayout為子控制元件分配空間的時候,獲取FrameLayout的LayoutParams的必須為LinearLayout.LayoutParams,而非FrameLayout.LayoutParams。
簡單的舉個例子說明一下:最外層有ReLativeLayout A,裡面有兩個LinearLayout B、C,而B中又有一個一個FrameLayout D。如果要在程式碼裡設定B的LayoutParams,B的LayoutParams要為RelativeLayout.LayoutParams。
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
B.setLayoutParams(params);
而D要設定的話,需要:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0);
params.weight = 8;
D.setLayoutParams(params);
這樣問題即可解決。還是自己有些基礎知識不牢固,寫到此。
相關文章
- java.lang.ClassCastException: com.alibaba.fastjson.JSONObject cannot be cast to com.JavaASTExceptionJSONObject
- Cause: java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.ibatis.mapping.MappedStatementJavaASTExceptionApacheBATAPP
- cannot be cast to java.lang.ComparableASTJava
- 解決String cannot be cast to java.util.List報錯ASTJava
- android.app.Application cannot be cast to 新建的ApplicationAndroidAPPAST
- Android ContextThemeWrapper cannot be cast to android.app.ActivityAndroidContextAPPAST
- 關於利用動態代理手寫資料庫連線池的異常 java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to java.sql.Connection資料庫JavaASTExceptionSQL
- SQL中欄位比較型別不匹配錯誤:‘cannot be cast to’SQL型別AST
- Android開發:LayoutParams的用法Android
- JAVA int 強制型別轉換錯誤提示(Cannot cast from Object to int)Java型別ASTObject
- com.alibaba.fastjson.JSONObject cannot be cast to XXX異常解決ASTJSONObject
- C++之static_cast,dynamic_cast,const_castC++AST
- 7.41 CASTAST
- java.lang.String cannot be cast to com.alibaba.fastjson.JSONObject解決辦法JavaASTJSONObject
- C++強制型別轉換:static_cast、dynamic_cast、const_cast、reinterpret_castC++型別AST
- teradata CASTAST
- oracle cast 用法OracleAST
- dynamic_castAST
- static_cast與dynamic_cast到底是什麼?AST
- reinterpret_cast 和 static_cast 的區別AST
- springboot mybatis Java.lang.ClassCastExceptionSpring BootMyBatisJavaASTException
- C++中static_cast, dynamic_cast, const_cast用法/使用情況及區別解析C++AST
- Laravel 5 Eloquent cast arrayLaravelAST
- Oracle的Cast的用法OracleAST
- oracle function函式castOracleFunction函式AST
- 型別轉換(cast)型別AST
- C++中dynamic_cast與static_cast淺析與例項演示C++AST
- static_cast和dynamic_cast的區別,看完這篇你就懂了!AST
- SQL中的cast()函式SQLAST函式
- const_cast的用法AST
- Oracle CAST函式不生效OracleAST函式
- Hadoop中java.lang.ClassCastException: partition解決方法HadoopJavaASTException
- Exception in thread "main" java.lang.ClassCastException: $Proxy13ExceptionthreadAIJavaAST
- Golang 型別轉換庫 castGolang型別AST
- substr,substrb,cast的使用、比較AST
- oracle 特殊SQL(TABLE( CAST( MULTISET()[zt]OracleSQLAST
- cast函式的用法案例AST函式
- (轉)Go 每日一庫之 castGoAST