所以我使用下面的代码来获取片段中的屏幕宽度,但是由于某些零点异常,我的应用程序不断崩溃,我无法弄清楚。
这是我正在使用的代码。
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
它说我崩溃了这一行:getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
不知道发生了什么,希望有人可以提供帮助!
这是我的堆栈跟踪:
FATAL EXCEPTION: main
Process: com.daprlabs.aaron.swipedeck2, PID: 25417
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.WindowManager android.support.v4.app.FragmentActivity.getWindowManager()' on a null object reference
at com.daprlabs.aaron.zivitApp.Main.FragmentDiscover$SwipeDeckAdapter.getView(FragmentDiscover.java:398)
at com.daprlabs.aaron.swipedeck.SwipeDeck.addNextView(SwipeDeck.java:215)
at com.daprlabs.aaron.swipedeck.SwipeDeck.onLayout(SwipeDeck.java:208)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5439)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5439)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5439)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5439)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1799)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5439)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5439)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5439)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5439)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5439)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5439)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5439)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2720)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5439)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2202)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1955)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1125)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6100)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.
05-03 15:47:52.762 1218-4087/? W/ActivityManager: Force finishing activity com.daprlabs.aaron.swipedeck2/com.daprlabs.aaron.zivitApp.Main.Main
答案 0 :(得分:0)
尝试使用此
//get screen size
public static int sizeScreen(){
return (int)((Resources.getSystem().getDisplayMetrics().widthPixels)/ Resources.getSystem().getDisplayMetrics().density);
}
//get screen height
public static int getHeight(Context context){
return context.getResources().getDisplayMetrics().heightPixels;
}
//get witdh screen
public static int getWidth(Context context){
return context.getResources().getDisplayMetrics().widthPixels;
}
确保使用onCreate而不是构造函数..