我正在安装MainFragment
的{{1}}的Android TV应用中工作,有时会在BrowseFragment
为onFocusSearch(View focused, int direction)
定义的方法中崩溃{1}}:
mOnFocusSearchListener
注意:发生崩溃的行标有BrowseFragment
。
此崩溃并不总是出现,但是当它表示private final BrowseFrameLayout.OnFocusSearchListener mOnFocusSearchListener =
new BrowseFrameLayout.OnFocusSearchListener() {
@Override
public View onFocusSearch(View focused, int direction) {
// if headers is running transition, focus stays
if (mCanShowHeaders && isInHeadersTransition()) {
return focused;
}
if (DEBUG) Log.v(TAG, "onFocusSearch focused " + focused + " + direction " + direction);
if (getTitleView() != null && focused != getTitleView() &&
direction == View.FOCUS_UP) {
return getTitleView();
}
if (getTitleView() != null && getTitleView().hasFocus() &&
direction == View.FOCUS_DOWN) {
return mCanShowHeaders && mShowingHeaders ?
mHeadersFragment.getVerticalGridView() :
mMainFragment.getView();
}
boolean isRtl = ViewCompat.getLayoutDirection(focused) == View.LAYOUT_DIRECTION_RTL;
int towardStart = isRtl ? View.FOCUS_RIGHT : View.FOCUS_LEFT;
int towardEnd = isRtl ? View.FOCUS_LEFT : View.FOCUS_RIGHT;
if (mCanShowHeaders && direction == towardStart) {
if (isVerticalScrolling() || mShowingHeaders) {
return focused;
}
return mHeadersFragment.getVerticalGridView();
} else if (direction == towardEnd) {
if (isVerticalScrolling()) {
return focused;
}
return mMainFragment.getView(); //*****CRASH******
} else {
return null;
}
}
};
为//*****CRASH******
时......
无法找到问题。救命啊!
更新leanback库会有帮助吗?
答案 0 :(得分:0)
在onFocusSearchListener
中调用onStart
后,应设置BrowseFragment
。崩溃很可能是因为mMainFragment
为null或者它返回的视图为null,因为它还没有实际添加。在调用onStart
时,将调用mMainFragment
生命周期方法(包括onCreateView
)。
看起来他们正在onCreate
中设置它。所以你可以做的就是抓住正在设置这个监听器的BrowseFrameLayout
,保存他们在其上设置的监听器,将监听器置空,然后将其设置回onStart
的子类{ {1}}。
在BrowseFragment
子类的onCreateView
中:
BrowseFragment
然后在 BrowseFrameLayout mBrowseFrame = (BrowseFrameLayout) v.findViewById(R.id.browse_frame);
OnFocusSearchListener mOnFocusSearchListener = mBrowseFrame.getOnFocusSearchListener();
mBrowseFrame.setOnFocusSearchListener(null);
子类的onStart
中:
BrowseFragment