-1

我正在使用MainFragment扩展的 Android TV 应用程序,有时它会在以下定义的方法中BrowseFragment崩溃:onFocusSearch(View focused, int direction)mOnFocusSearchListenerBrowseFragment

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******。这种崩溃并不总是出现,但是当它确实说那mMainFragmentnull......

找不到问题。帮助!

更新leanback库,会有帮助吗?

4

1 回答 1

0

onFocusSearchListener应该在被onStart调用 之后设置BrowseFragment。崩溃很可能是因为mMainFragment它是 null 或者它返回的视图是 null 因为它实际上还没有被添加。到调用的时候onStartmMainFragment生命周期方法将被调用(包括onCreateView)。

看起来他们正在将其设置为onCreate. 所以你可以做的是抓住BrowseFrameLayout这个监听器被设置,保存他们设置的监听器,清空监听器,然后onStart在你的子类中重新设置它BrowseFragment

onCreateView您的BrowseFragment子类中:

    BrowseFrameLayout mBrowseFrame = (BrowseFrameLayout) v.findViewById(R.id.browse_frame);
    OnFocusSearchListener mOnFocusSearchListener = mBrowseFrame.getOnFocusSearchListener();
    mBrowseFrame.setOnFocusSearchListener(null);

然后在onStart你的BrowseFragment子类中:

   mBrowseFrame.setOnFocusSearchListener(mOnFocusSearchListener);
于 2016-10-03T02:14:45.497 回答