【问题标题】:Using window in not Activity class?在非活动类中使用窗口?
【发布时间】:2012-12-19 15:39:16
【问题描述】:

之前,我看到这个sn-p找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 类中使用这个sn-p。所以我将我的Activity 实例发送到AsyncTask 类,在onCreate 的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;

并且应用程序运行良好。所以看起来

window.getDecorView().getWindowVisibleDisplayFrame(rectgle);

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

【问题讨论】:

    标签: android android-activity window statusbar titlebar


    【解决方案1】:

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

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-05
      • 2011-12-01
      相关资源
      最近更新 更多