リンク内容を参考にコンテンツ領域のサイズを取得しようとしています。
下記エラーが出てしまいます。
おそらくrelative layoutの初期化?ができていないのだと思いますが、どこが間違っていますでしょうか。
class CustomView extends SurfaceView implements SurfaceHolder.Callback, Runnable { //ーーー中略ーーー //コンストラクタ public CustomView(Context context) { super(context); // SurfaceView描画に用いるコールバックを登録する。 getHolder().addCallback(this); // 描画用の準備 paint = new Paint(); paint2 = new Paint(); paint3 = new Paint(); paint4 = new Paint(); paint5 = new Paint(); paint6 = new Paint(); //ーーー中略ーーー } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); // Viewサイズを取得する RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout1); view_w =rl.getWidth(); //コンテンツ領域のサイズをview_wに入れたい view_h =rl.getHeight(); Log.d("width", "view_w:" +view_w); Log.d("height", "view_h:" +view_h); } //ーーー中略ーーー } }
エラー
Process: com.example.blockbreak, PID: 21979 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RelativeLayout.getWidth()' on a null object reference at com.example.blockbreak.MainActivity$CustomView.onWindowFocusChanged(MainActivity.java:105)
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。