findViewById返回gridLayout導致異常解決

以下語句導致了程序異常,

GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout);

參考鏈接,在XML中定義的gridLayout與在程序中定義的gridLayout不一致,導至了程序異常,改爲一致的定義,問題解決。

程序中的定義爲:
import android.widget.GridLayout;

然而XML中的定義爲:
androidx.gridlayout.widget.GridLayout

解決方案:
程序改爲以下:

androidx.gridlayout.widget.GridLayout gridLayout = findViewById(R.id.gridLayout);

額外的問題,layout如果重疊,如何調整不同layout的顯示順序,在Android Studio中的Component Tree裏調整先後順序,就可以調整layout顯示的優先級。

Reference

App crashing in the part of Grid layout declaration

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章