gpt4 book ai didi

android - 在非 Activity 类中使​​用窗口?

转载 作者:太空狗 更新时间:2023-10-29 15:14:02 26 4
gpt4 key购买 nike

之前,我看到这段代码可以找到 Height of statusbar?(and also Tiltebar) here :

Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop=
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;

昨天,我试图在 AsyncTask 类中使用这个 fragment 。所以我在 onCreate 中将我的 Activity 实例发送到 AsyncTask 类 Activity :

LongOperation loper = new LongOperation(this, vg, vArray, hintsArray,x_positionArray, y_positionArray);//Here `this` refers to current Activity
loper.execute("YES");

在 AsynkTask 类的 doInBackground 中我只做了:

Thread.sleep(3000); 

onPostExecute 方法中,我复制上面的代码来计算状态栏和标题栏的高度。但是应用程序崩溃了,没有任何有用的 logcat 登录。然后我评论了一些这样的行:

Rect rectgle = new Rect();
Window window = activity.getWindow();
// window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
// int StatusBarHeight= rectgle.top;
contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT)
.getTop();
// int TitleBarHeight= contentViewTop - StatusBarHeight;

App 运行良好。看来

window.getDecorView().getWindowVisibleDisplayFrame(rectgle);

导致App崩溃,但我不知道为什么?我做错了什么?

最佳答案

好吧,如果您在 Android 2.3.3 中运行您的应用程序,那么应用程序可能会崩溃。因为这种方法有点损坏。所以 this解决方案可能对您有所帮助。

希望这有帮助:)

关于android - 在非 Activity 类中使​​用窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14170029/

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