在StaggeredGridView中获取mScrollListener侦听器null

时间:2015-08-31 12:13:38

标签: android staggered-gridview

我已经在我的应用程序中实现了一个自定义网格视图。我正在使用StaggeredGridView。我能够在我的代码中实现它但现在当我尝试滚动网格视图时,我的应用程序崩溃,因为它获得 mScrollListener null 我没有得到滚动侦听器如何获取空值。

这是我的代码:

public class CompareSelectorGridFragments extends Fragment {
    private List<StaggeredGridViewItem> itemlist;
    private List<catagoriesListModal> _categories;
    private StaggeredGridView mStaggeredView;
    private View mRootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Session.ChangeFragmentOnBackPressHandlerWithBack(null, null, "", 0);
        Session.getmCountImage().setvisibilityOff();
        itemlist = new ArrayList<StaggeredGridViewItem>();
        mRootView = inflater.inflate(
                R.layout.fragment_product_selector_for_compare, container,
                false);
        if (getResources().getBoolean(R.bool.landscape_only)) {
            getActivity().setRequestedOrientation(
                    ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            getActivity().setRequestedOrientation(
                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        initUI(mRootView);
        return mRootView;
    }

    private void initUI(View rootView) {
        mStaggeredView = new StaggeredGridView(getActivity());
        mStaggeredView.initialize(3, StaggeredGridView.Mode.FIXED);
        mStaggeredView.setOnScrollListener(scrollListener);
        mStaggeredView = (StaggeredGridView) rootView
                .findViewById(R.id.grid_product_selector);
        StaggeredGridViewItem item = null;
        for (int i = 0; i < DummyData.getSelectorList().size(); i++) {
            _categories = DummyData.getSelectorList().get(i).getCategories();
            item = new ProductSelectorCompareAdapter(getActivity(), DummyData
                    .getSelectorList().get(i).getName(), DummyData
                    .getSelectorList().get(i).getImage(), _categories);

            itemlist.add(item);

        }
        mStaggeredView.addItemsList(itemlist);

    }

    private OnScrollListener scrollListener = new OnScrollListener() {
        public void onTop() {
        }

        public void onScroll() {

        }

        public void onBottom() {
        }
    };

}

以下是适配器的代码:

public class SelectorCompareAdapter extends StaggeredGridViewItem {

    Activity activity;
    List<SelectorTest> listSelector;
    private Button btn;
    List<catagoriesListModal> mCategories;
    private View mView;
    private int mHeight;
    private String Name;
    private int Image;

    public SelectorCompareAdapter(Activity activity, String name,
            int image, List<catagoriesListModal> _categories) {
        this.activity = activity;
        this.Name = name;
        this.Image = image;
        this.mCategories = _categories;
    }

    static class ViewHolder {
        TextView tvProductName;
        ImageView imgView;
        View viewRightLine;
        ScrollView scrlLstItems;
        LinearLayout lnrLayout;
    }

    @Override
    public View getView(LayoutInflater inflater, ViewGroup parent) {
        final ViewHolder viewHolder;
        mView = inflater.inflate(R.layout.selector_item_compare, null);
        if (mView != null) {
            viewHolder = new ViewHolder();
            viewHolder.tvProductName = (TextView) mView
                    .findViewById(R.id.tv_product);
            viewHolder.imgView = (ImageView) mView
                    .findViewById(R.id.img_product);
            viewHolder.viewRightLine = (View) mView
                    .findViewById(R.id.view_right_line);
            viewHolder.lnrLayout = (LinearLayout) mView
                    .findViewById(R.id.lnr_test);
            viewHolder.tvProductName.setText(Name);
            viewHolder.imgView.setImageResource(Image);
            viewHolder.imgView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    viewHolder.lnrLayout.setVisibility(View.VISIBLE);
                    viewHolder.imgView.setVisibility(View.GONE);
                    for (int i = 0; i < mCategories.size(); i++) {

                        btn = new Button(activity);
                        btn.setText(mCategories.get(i).getName());

                        viewHolder.lnrLayout.addView(btn);
                        int idx = viewHolder.lnrLayout.indexOfChild(btn);
                        btn.setTag(Integer.toString(idx));

                        btn.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                String idxStr = (String) v.getTag();
                                Toast.makeText(activity, idxStr,
                                        Toast.LENGTH_SHORT).show();

                            }
                        });
                    }
                }
            });
            viewHolder.tvProductName.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (activity.getResources().getBoolean(
                            R.bool.landscape_only)) {
                        viewHolder.lnrLayout.removeAllViews();
                        viewHolder.lnrLayout.setVisibility(View.GONE);
                        viewHolder.imgView.setVisibility(View.VISIBLE);
                    }

                }
            });
            mView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) mView.getTag();
        }
        return mView;
    }

    @Override
    public int getViewHeight(LayoutInflater inflater, ViewGroup parent) {
        LinearLayout item_containerLinearLayout = (LinearLayout) mView
                .findViewById(R.id.ll_container);
        item_containerLinearLayout.measure(MeasureSpec.UNSPECIFIED,
                MeasureSpec.UNSPECIFIED);
        mHeight = item_containerLinearLayout.getMeasuredHeight();
        return mHeight;
    }
}

记录错误: -

08-31 08:37:28.946: E/AndroidRuntime(5076): java.lang.NullPointerException
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at com.mss.skyjack.customgridview.StaggeredGridView.onScrollChanged(StaggeredGridView.java:243)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.View.scrollTo(View.java:10210)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.widget.ScrollView.onOverScrolled(ScrollView.java:763)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.View.overScrollBy(View.java:16879)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.widget.ScrollView.onTouchEvent(ScrollView.java:634)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.View.dispatchTouchEvent(View.java:7384)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2205)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1940)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.View.dispatchPointerEvent(View.java:7564)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:174)
08-31 08:37:28.946: E/AndroidRuntime(5076): 	at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImp

1 个答案:

答案 0 :(得分:1)

或者只是在findViewById调用之后尝试调用setScrollListener