for (String s : chunks) {
String possibleSquare = "s" + s.substring(2, 4);
int id = boardContext.getResources().getIdentifier(possibleSquare, "id", boardContext.getPackageName());
ImageView backgroundImg = (ImageView) findViewById(id);
backgroundImg.setBackgroundColor(Color.rgb(255, 255, 255));
}
相关代码如上。它来自类"Pawn"中的一个方法,该方法由另一个类"棋盘"中的代码调用。现在,findViewById带有下划线,AndroidStudio告诉我它"无法解析方法findViewById"。
我认为这是因为代码不知道在哪里搜索视图,但我不确定如何指示它应该在我的"Board.XML"文件中搜索。
您需要
使用R.id.YOUR_ID而不仅仅是 ID。
例如
ImageView backgroundImg = (ImageView) findViewById(R.id.id);
您可能在片段中调用findViewById,或者调用没有findViewById方法的独立类等。所以你需要把它传递给类。
尝试:
boardContext.findViewBy(R.id.id);
for (String s : chunks) {
String possibleSquare = "s" + s.substring(2, 4);
int id = boardContext.getResources().getIdentifier(possibleSquare, "id", boardContext.getPackageName());
ImageView backgroundImg = (ImageView) boardContext.findViewById(id);
backgroundImg.setBackgroundColor(Color.rgb(255, 255, 255));
}