画面サイズに関係なく、背景画像 (image1) に対して同じ位置とサイズになるように、画面上の画像 (image2) のサイズと位置を設定しようとしています。これを行うには、クラスのonCreate
メソッドでimage2 のレイアウト パラメーターを変更します。Activity
public class Game_main extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ImageView image2= (ImageView) findViewById(R.id.seed1);
ImageView image1= (ImageView) findViewById(R.id.board_rev1);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rlGame);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((image1.getWidth())/4, (image2.getHeight())/4);
params.leftMargin = (image1.getWidth())/2;
params.topMargin = (image1.getHeight())/4;
rl.addView(image2, params);
setContentView(R.layout.gamerev);
}
}
しかし、アプリがこのアクティビティに移動すると、アプリケーションが予期せず停止したというエラーが表示されます。次に、アプリを強制終了する必要があります。なぜこれが起こるのでしょうか?足りないものはありますか?またはこれを行うためのより良い方法はありますか?
logcat のエラーは次のとおりです。
E/AndroidRuntime(296): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.soft.tobi/com.soft.tobi.Game_main}: java.lang.NullPointerExc e ption
しかし、すべての ID が xml ファイルに存在するため、何も null になる理由がわかりません。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rlGame"
>
<ImageView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/board_rev1" android:src="@drawable/board_rev1"></ImageView>
<ImageView android:id="@+id/seed1" android:src="@drawable/seed1" android:adjustViewBounds="true" android:layout_height="200dp" android:layout_marginTop="165dp" android:layout_width="200dp" android:layout_marginLeft="30dp"></ImageView>
</RelativeLayout>
ありがとう