--fragment will result in an error unless getActivity (). getPackageName () is used. --If you use Integer.parseInt (), you get a java.lang.NumberFormatException error.
MainActivity.java
//java.lang.I get a NumberFormatException error
String bookImage = "book_image";
int imageId = Integer.parseInt(bookImage);
MainActivity.java
String bookImage = "book_image";
int imageId = getResources().getIdentifier(
bookImage, "drawable", getPackageName());
-----------------------------
//Output result
bookImage : book_image
imageId : 2131099756 (Some number)
MainFragment.java
String bookImage = "book_image";
int imageId = getResources().getIdentifier(
bookImage, "drawable", getActivity().getPackageName());
-----------------------------
//Output result
bookImage : book_image
imageId : 2131099756(Some number)
Recommended Posts