getSize()给我错误

时间:2012-03-11 10:03:38

标签: android android-layout

当我实施

WindowManager wm = ((WindowManager)context.getSystemService(context.WINDOW_SERVICE));
Display display = wm.getDefaultDisplay();
m_nDisplayWidth = display.getWidth();
m_nDisplayHeight    = display.getHeight();

我可以正常运行,但是当我实现getSize时,我得到了runtimeError

Point size = new Point();
display.getSize(size); //Error right here
m_nDisplayWidth = size.x;
m_nDisplayHeight = size.y;

logcat的:

03-11 01:45:25.865: E/AndroidRuntime(18835): FATAL EXCEPTION: main 
03-11 01:45:25.865: E/AndroidRuntime(18835): android.view.InflateException: Binary XML file line #7: Error inflating class com.brain.development.GameRun
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:215) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.app.Activity.setContentView(Activity.java:1679) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at com.brain.development.BrainDevelopmentActivity$1.onClick(BrainDevelopmentActivity.java:25) 03-11 01:45:25.865: E/AndroidRuntime(18835):  at android.view.View.performClick(View.java:2582) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.View$PerformClick.run(View.java:9252) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.os.Handler.handleCallback(Handler.java:587) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.os.Handler.dispatchMessage(Handler.java:92) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.os.Looper.loop(Looper.java:130) 03-11 01:45:25.865: E/AndroidRuntime(18835):     at android.app.ActivityThread.main(ActivityThread.java:3691) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at java.lang.reflect.Method.invokeNative(Native Method) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at java.lang.reflect.Method.invoke(Method.java:507) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at dalvik.system.NativeStart.main(Native Method) 
03-11 01:45:25.865: E/AndroidRuntime(18835): Caused by: java.lang.reflect.InvocationTargetException 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at java.lang.reflect.Constructor.constructNative(Native Method) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.LayoutInflater.createView(LayoutInflater.java:505) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    ... 19 more 
03-11 01:45:25.865: E/AndroidRuntime(18835): Caused by: java.lang.NoSuchMethodError: android.view.Display.getSize 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at com.brain.development.GameRun$GameThread.<init>(GameRun.java:46) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at com.brain.development.GameRun.<init>(GameRun.java:97) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    ... 22 more

7 个答案:

答案 0 :(得分:70)

这支持较旧和较新的设备:

private static Point getDisplaySize(final Display display) {
    final Point point = new Point();
    try {
        display.getSize(point);
    } catch (java.lang.NoSuchMethodError ignore) { // Older device
        point.x = display.getWidth();
        point.y = display.getHeight();
    }
    return point;
}

答案 1 :(得分:4)

获取所有设备的显示大小的方法:

private static Point getDisplaySize(final Display display) {
Point point = new Point();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2){   //API LEVEL 13
     display.getSize(point);
}else{    
    point.x = display.getWidth();
    point.y = display.getHeight();
}
return point;
    }

gettin尺寸:

    WindowManager wm = ((WindowManager)context.getSystemService(context.WINDOW_SERVICE));
    Display display = wm.getDefaultDisplay();
   Point myPoint = getDisplaySize(display); 
   Log.i("***Size X::", String.valueOf(myPoint.x));
   Log.i("***Size Y::", String.valueOf(myPoint.y));

答案 2 :(得分:2)

此版本适用于API&lt; 4,也是:

@SuppressWarnings("deprecation")
@TargetApi(VERSION_CODES.HONEYCOMB_MR2)
public static Point getSize(Display display) {
    Point point = new Point();
    if (sdkInt() >= VERSION_CODES.HONEYCOMB_MR2) {
        display.getSize(point);
    } else {
        point.set(display.getWidth(), display.getHeight());
    }
    return point;
}

@SuppressWarnings("deprecation")
public static int sdkInt() {
    return Integer.parseInt(VERSION.SDK);
}

答案 3 :(得分:0)

尝试使用它:

Display display = getWindowManager().getDefaultDisplay();

没有它,我在这段代码中看不到任何问题...

答案 4 :(得分:0)

试试这个,它对我有用。

getWindowManager().getDefaultDisplay().getWidth();

答案 5 :(得分:0)

很抱歉,我迟到了,但要确保你使用的是Android sdk 13或更高版本。

答案 6 :(得分:0)

这是实用方法,你可以利用:

public static int getWidth(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        Point size = new Point();
        int width;
        if (Build.VERSION.SDK_INT >= 13) {
            display.getSize(size);
            width = size.x;

        } else {
            width = display.getWidth();
        }
        return width;
    }