【问题标题】:androidx.gridlayout.widget.GridLayout cannot be cast to android.widget.GridLayoutandroidx.gridlayout.widget.GridLayout 无法转换为 android.widget.GridLayout
【发布时间】:2019-12-05 13:02:06
【问题描述】:

我遇到错误

“引起:java.lang.ClassCastException: androidx.gridlayout.widget.GridLayout 无法转换为 android.widget.GridLayout”,在 mainActivity.java

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

 for(int i=0; i<mygridLayout.getChildCount(); i++)
 {
     ((ImageView) mygridLayout.getChildAt(i)).setImageResource(0);
 }

【问题讨论】:

  • 您必须使用AndroidX 库或Support 库。添加有关您的布局和代码的更多详细信息

标签: java android android-activity androidx


【解决方案1】:

改变

import android.widget.gridLayout 

import androidx.gridlayout.widget.GridLayout;

你的代码可以工作

【讨论】:

    【解决方案2】:

    在代码中使用它会起作用。

    androidx.gridlayout.widget.GridLayout mygridLayout = findViewById(R.id.gridLayout);
    
    for(int i=0; i<mygridLayout.getChildCount(); i++){
     ((ImageView) mygridLayout.getChildAt(i)).setImageResource(0);
    } 
    

    【讨论】:

    • 太好了,谢谢,它成功了! :) 你在哪里找到的?
    【解决方案3】:

    您正在 layout.xml 中使用 AndroidX 包中的 GridLayout,并且您正在源代码中从包 android.widget 中导入 Gridlayout。使用其中一种并使它们保持一致。

    【讨论】:

      【解决方案4】:

      你应该把你的代码改成这样:

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

      【讨论】:

        【解决方案5】:

        为此,我们需要导入"androidx.gridlayout.widget.GridLayout;" 而不是"android.widget.GridLayout;"

        【讨论】:

          猜你喜欢
          • 2018-07-23
          • 1970-01-01
          • 1970-01-01
          • 2023-02-02
          • 2016-06-30
          • 2016-01-14
          • 2013-07-25
          • 2016-07-05
          • 2017-01-24
          相关资源
          最近更新 更多