在BrowseFragment中对onFocusSearch进行NullPointerException?

时间:2016-10-02 21:00:51

标签: android android-studio android-fragments android-tv leanback

我正在安装MainFragment的{​​{1}}的Android TV应用中工作,有时会在BrowseFragmentonFocusSearch(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库会有帮助吗?

1 个答案:

答案 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