0
我想在片段活動的imageview中實現隨機圖像。我受到了堆棧的社區問題的啓發,但沒有一個用於碎片。片段中ImageView中的隨機圖像
下面是代碼:
public class OneFragment extends Fragment {
final Random rnd = new Random();
ImageView img;
public OneFragment() {
// Required empty public constructor
}
private static TextView creditWallet;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static void onUpdateView(Context aiContext) {
// TODO Auto-generated method stub
if (aiContext != null && creditWallet != null)
creditWallet.setText(PreferenceConnector.readInteger(aiContext, PreferenceConnector.WALLETPOINTS, 0) + "");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
super.onViewCreated(view, savedInstanceState);
// Inflate the layout for this fragment
final ImageView img = (ImageView) view.findViewById(R.id.imgRandom);
return view;
// I have 3 images named img_0 to img_2, so...
final String str = "img_" + rnd.nextInt(2);
img.setImageDrawable
(
getResources().getDrawable(getResourceID(str, "drawable",
getApplicationContext()))
);}
protected final static int getResourceID
(final String resName, final String resType, final Context ctx) {
final int ResourceID =ctx.getResources().
getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0) {
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
} else {
return ResourceID;
}
}
}
但我有在這一行錯誤:
final String str = "img_" + rnd.nextInt(2);
不到的語句
問題出在哪裏?