-1

所以即時使用下面的代碼來獲取我的屏幕寬度在一個片段,但我的應用程序不斷崩潰,由於一些零點異常,我無法弄清楚。如何獲取片段中的屏幕寬度?

這是我使用的代碼。

  DisplayMetrics displayMetrics = new DisplayMetrics(); 
      getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
      int height = displayMetrics.heightPixels; 
      int width = displayMetrics.widthPixels; 

它說IM崩潰在這條線: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

的可能的複製[什麼是一個NullPointerException ,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – csmckelvey

+0

'試圖調用虛擬方法' android.view.WindowManager android.support.v4.app.FragmentActivity.getWindowManager()'在null對象引用上getActivity()返回null。 – csmckelvey

回答

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 ..

+0

你是指片段活動中的onCreateView()或onCreate()嗎?你是什​​麼意思「使用onCreate() – TheQ