0
私のフラグメントアクティビティでイメージビューにランダムイメージを実装したかったのです。私はスタックのコミュニティの質問に触発されていますが、断片のものはありませんでした。ここでフラグメント内の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);
到達不能の文
問題があるの?あなたはonCreateView
関数の最後にreturn
ステートメントを移動する必要があるので、他にこのreturn view;
何も後