gpt4 book ai didi

android - getSize() 给我错误

转载 作者:IT老高 更新时间:2023-10-28 23:26:25 26 4
gpt4 key购买 nike

当我实现时

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

最佳答案

这支持旧设备和新设备:

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;
}

关于android - getSize() 给我错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9654016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com