このコードは実用的ではないように見えるかもしれませんが、答えをよりよく理解できるように単純化しました。null ポインター例外が発生しましたが、その理由がわかりません。drawableフォルダーにpresident1.pngがあります。「president1」については、president1.png も試しました。メソッド FlipCard(View v) は、android:onClick="flipCard" でボタンを押すことによって呼び出されています。助言がありますか?
public void flipCard(View v){
card1.setImageResource(getIdentifier(getBaseContext()));
}
public static int getIdentifier(Context context){
return context.getResources().getIdentifier("president1", "drawable", context.getPackageName());
}
<ImageButton
android:id="@+id/card1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/card8"
android:layout_toLeftOf="@+id/card2"
android:background="@null"
android:onClick="flipCard"
android:src="@drawable/facedown" />