我想在软键盘弹出窗口中隐藏操作栏。我使用以下代码的代码来获得结果。但当我想回去时,应用程序崩溃了。我已经包含在哪一行我也得到了错误。请指导我完成这个。
提前致谢。
private void createView(){
mRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
//rect will be populated with the coordinates of your view that area still visible.
mRootView.getWindowVisibleDisplayFrame(rect);
int heightDiff = mRootView.getRootView().getHeight() - (rect.bottom - rect.top);
if (heightDiff > 100) {
// if more than 100 pixels, its probably a keyboard...
//keyboard visible
mBinding.healthRecordContainer.setVisibility(View.GONE);
((AppCompatActivity) getActivity()).getSupportActionBar().hide();
} else {
//keyboard not visible
mBinding.healthRecordContainer.setVisibility(View.VISIBLE);
((AppCompatActivity) getActivity()).getSupportActionBar().show(); // app crashes
} //java.lang.NullPointerException
}
});
}
答案 0 :(得分:0)
在您宣布活动的清单中只做一件事。 例如。
<activity
android:name=".Your Activity here"
android:windowSoftInputMode="stateVisible|adjustResize"
>
</activity>
答案 1 :(得分:0)
您可以使用以下代码。然后app不会崩溃。
Activity activity = getActivity();
if (activity instanceof ActionBarActivity) {
ActionBarActivity actionBarActivity = (ActionBarActivity) activity;
ActionBar actionBar = actionBarActivity.getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
}