リンク内容
を参考にしています。
コンテンツ領域のサイズを取得したいのですが、今ひとつやり方がわからないのでご教授いただけると幸いです。
ゲームをつくっているため、 CustomViewを使用しています。
onCreate内で
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //カスタムビュークラスのインスタンスをコンテントにセット setContentView(new CustomView(this)); }
としています。
CustomViewクラス内に
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); CustomView cmView = null; Point viewSize = getViewSize(cmView); int width = viewSize.x; int height = viewSize.y; Log.d("size", "width: "+width); Log.d("size", "height: "+height); }
としましたが、NullPointerExceptionが出ます。
下記がわからないのが原因だと思います。
・CustomView cmViewをnullじゃなくするにはどうしたらいいのでしょうか?
・CustomView cmViewをfindViewByIdすればいいのでしょうか?
・また、その際CustomView cmViewにどうやてidをつければいいのでしょうか?
putIDみたいなメソッドをつかう?
layout.xmlの方をいじる?
よろしくおねがいします。
エラー
Process: com.example.blockbreak, PID: 28307 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.blockbreak/com.example.blockbreak.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getWidth()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2320) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2380) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5289) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getWidth()' on a null object reference at com.example.blockbreak.MainActivity$CustomView.<init>(MainActivity.java:96) at com.example.blockbreak.MainActivity.onCreate(MainActivity.java:30) at android.app.Activity.performCreate(Activity.java:6018) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2273) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2380) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5289) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
追記
これまで画面サイズを取得してた方法
WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE); Display disp = wm.getDefaultDisplay(); Point size = new Point(); disp.getSize(size); view_w =size.x;//はば view_h =size.y;
追記
CustomViewのコンストラクタ内で呼びたいのですが、呼び出すと、値がゼロになってしまうのですが、なぜでしょか?
CustomView extends SurfaceView(){ int width,height; @Override public void onWindowFocusChanged(boolean hasFocus) { { super.onWindowFocusChanged(hasFocus); width = getViewSize(this).x;//720 height =getViewSize(this).y;//1230 //ここでは値がとれてる } } CustomView(){ //コンストラクタ //ここで呼びたい width = getViewSize(this).x; height = getViewSize(this).y; //ゼロになる。 } }
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/16 05:57
2016/05/16 14:48
2016/05/17 09:13
2016/05/17 09:58 編集
2016/05/18 05:30
2016/05/18 06:27