Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 调用findViewById时出现问题_Java_Android_Findviewbyid - Fatal编程技术网

Java 调用findViewById时出现问题

Java 调用findViewById时出现问题,java,android,findviewbyid,Java,Android,Findviewbyid,相关代码见上文。它来自另一个类“Chessboard”中的代码调用的类“Pawn”中的方法。现在,findViewById加了下划线,AndroidStudio告诉我它“无法解析方法findViewById” 我认为这是因为代码不知道在哪里搜索视图,但我不确定如何指示它应该在我的“Board.XML”文件中搜索 您需要使用R.id.YOUR_id,而不仅仅是id. 比如说 for (String s : chunks) {

相关代码见上文。它来自另一个类“Chessboard”中的代码调用的类“Pawn”中的方法。现在,findViewById加了下划线,AndroidStudio告诉我它“无法解析方法findViewById”


我认为这是因为代码不知道在哪里搜索视图,但我不确定如何指示它应该在我的“Board.XML”文件中搜索

您需要使用R.id.YOUR_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) findViewById(id);                                                   
    backgroundImg.setBackgroundColor(Color.rgb(255, 255, 255));                                               

}              

您可能在片段或独立类等中调用findviewbyd,这些片段或类没有findviewbyd方法。所以你需要把它传给全班

尝试:


典当是碎片吗?这绝对不是一项活动。是否有必要拥有一组不同的imageview,或者您可以拥有一个imageview,并在需要时将不同的图片传递给它?我已经尝试过了。它返回错误“无法解析符号”id“是您的imageview“id”的id吗?你能将XML添加到你的帖子中吗?不,我有多个id为“sXX”的ImageView,其中每个X都是0到7之间的数字。id表示这些imageview中的每一个。您需要将id设置为XML中的id。是的,代码boardContext.getResources().getIdentifier(可能是quare,“id”,boardContext.getPackageName());这正是我之前所想的,但它仍然给了我错误‘无法解析方法findviewbyd(int)’嗯,你能发布上下文中传递的内容吗?案例“black_pawn”:pawn blackPawn=new pawn(v.getContext());blackPawn.possibleMoves(1,0);该代码位于onclick侦听器中。视图V是我的XML中的imageView您介意简单地发布创建典当的父活动/片段吗
ImageView backgroundImg = (ImageView) findViewById(R.id.id);                                                   
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));                                               

}