findViewById 与 getIdentifier 不工作

findViewById with getIdentifier not working

我正在遍历一个数组,在那个 for 循环中我还使用这个数组来获取几个微调器的 ID:

Integer[] ids;
ids = new Integer[8];
ids[0] = R.id.nr_area;
ids[1] = R.id.nr_municipality;
ids[2] = R.id.nr_method;
ids[3] = R.id.nr_bait;
ids[4] = R.id.nr_sessionhours;
ids[5] = R.id.nr_bifangst_art;
ids[6] = R.id.nr_bifangst_released;
ids[7] = R.id.nr_name;

在 for 循环中:

for (int i = 0; i < fields.length; i++) {
if(fields[i].contains("*") || fields[i].matches("")){

 String test = ids[i].toString();
 int resID = getResources().getIdentifier(test, "id", getPackageName());

 Spinner spnr = (Spinner) findViewById(resID); //error is on this row
 spnr.setBackgroundColor(Color.GREEN);
 }
}

现在我得到的错误如下:

android.support.v7.internal.widget.TintEditText cannot be cast to android.widget.Spinner

所以我想这不能与微调器一起使用,因为我找到了 EditText 的示例代码..但是我该如何做我想用微调器做的事情?

如有任何帮助,我将不胜感激!已经为此奋斗了几个小时。非常感谢..

我认为问题是其中一个 id 是 EditText 或 id 是 EditText 并且您最近在 xml 中将其更改为 Spinner。确保 ID 正确,将它们与 xml 相关联,如果问题仍然存在,请尝试再次清理项目以进行索引,应该可以。

希望这对您有所帮助...