以前、私はHeight of statusbar?(and also Tiltebar)
ここで見つけるためにこのスニペットを見ました:
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
ので、アクティビティのインスタンスをアクティビティのAsyncTask
クラスに送信しますonCreate
。
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);
アプリがクラッシュする原因になりますが、理由はわかりません。何か間違ったことをしましたか?