getIdentifier没有提供正确的ID

时间:2019-11-16 12:21:43

标签: java android view

所以我有更多像这样的imageView:

  

imageView1

     

...

     

imageView8

int defendPosition = defend(); // This is random generated number from 1-8;
String imageID = "imageView" + defendPosition;
int resID = getResources().getIdentifier(imageID, "id", getPackageName());
Log.i("resID", " "+ resID); // it logs value is 0
secondMove = findViewById(resID);
secondMove.setTranslationY(-1500); // When I try to run this, it says null refference;

我需要获取正确的ID

1 个答案:

答案 0 :(得分:0)

尝试

int resID=this.getResources().getIdentifier(imageID,"id",getActivity().getPackageName())