带有 ViewHolder 的 GridView 在滚动时崩溃

问题描述 投票:0回答:2

我有一个 gridView,在我使用 AstickyHeader 库之前它工作得很好。 实施 AstickyHeader Library 后,我的应用程序在滚动网格视图时崩溃。

我的 ViewHolder 似乎存在获取空对象引用的问题。但之前效果很好。非常感谢任何帮助。

这是我的 GridViewAdapter 的 getView() 方法。

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
       // holder = null;

        Log.e("position",position+"");
        Product productItem = (Product) productList.get(position);

        if (row == null) {
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(R.layout.products_grid_item_layout, parent, false);
                holder = new ViewHolder();
                holder.productImage = (ImageView) row.findViewById(R.id.productImage);
                holder.productImageClone = (ImageView) row.findViewById(R.id.productImageClone);
                holder.detailsIcon = (ImageView) row.findViewById(R.id.detailsIcon);
                holder.productTitle = (TextView) row.findViewById(R.id.productTitle);
                holder.productSubTitle = (TextView) row.findViewById(R.id.productSubTitle);
                holder.productQuantity = (TextView) row.findViewById(R.id.productQuantity);
                holder.priceDollar = (TextView) row.findViewById(R.id.priceDollar);
                holder.priceCent = (TextView) row.findViewById(R.id.priceCent);
                holder.productCount = (TextView) row.findViewById(R.id.productCount);
                holder.productGridLayout = (RelativeLayout) row.findViewById(R.id.productGridLayout);

                row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
            Log.e("position tag",position+"");
        }

        String price = productItem.getPrice().toString();

        String[] pricearray;
        pricearray =  price.split("\\.");

        holder.productTitle.setText(productItem.getTitle().toString());
        holder.productSubTitle.setText(productItem.getSubtitle().toString());
        holder.productQuantity.setText(productItem.getVolume().toString());
        holder.priceDollar.setText(pricearray[0]+".");
        holder.priceCent.setText(pricearray[1]);


        if(productItem.getInCart()) {
            holder.productCount.setVisibility(View.VISIBLE);
            holder.productCount.setText(productItem.getVolume());
        }

        Picasso.with(context)
                .load(productItem.getImageUrl())
                .placeholder(R.drawable.favourites)
                .error(R.drawable.favourites)
                .into(holder.productImage);

        Picasso.with(context)
                .load(productItem.getImageUrl())
                .placeholder(R.drawable.favourites)
                .error(R.drawable.favourites)
                .into(holder.productImageClone);

        urlSlug = productItem.getUrlSlug();


        holder.detailsIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final int position = currentGridView.getPositionForView(v);
                Log.e("grid position", position+"");

                Intent productDetailIntent = new Intent(context,ProductDetailActivity.class);
                productDetailIntent.putExtra("url_slug", productList.get(position).getUrlSlug());

                context.startActivity(productDetailIntent);
            }

        });

        return row;
    }

这是product_grid_item_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/productGridLayout"
    android:layout_width="wrap_content"
    android:layout_height="180dp"
    android:background="@drawable/categories_grid_color_selector"
    >


    <ImageView
        android:id="@+id/demeterImage"
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:visibility="invisible"
        android:layout_centerHorizontal="true"
        />



    <ImageView
        android:id="@+id/productImageClone"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:src="@drawable/fav"
        android:layout_centerHorizontal="true"
        />

    <ImageView
        android:id="@+id/productImage"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:src="@drawable/fav"
        android:layout_centerHorizontal="true"
        />

    <TextView
        android:id="@+id/productCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:layout_alignBottom="@id/productImage"
        android:background="@color/green"
        android:textColor="@color/white"
        android:padding="4dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="@dimen/margin_16"
        android:visibility="invisible"/>

    <TextView
        android:id="@+id/productDiscount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="10% KORTING"
        android:layout_alignBottom="@id/productImage"
        android:background="@color/pink"
        android:textColor="@color/white"
        android:padding="4dp"
        android:layout_toRightOf="@id/productCount"
        android:layout_marginLeft="8dp"
        android:visibility="invisible"/>




    <LinearLayout
        android:id="@+id/productDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@id/productImage"
        android:layout_alignParentLeft="true"
        android:paddingLeft="@dimen/margin_16">

        <TextView
        android:id="@+id/productTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text = "Product title"
        android:textColor="@color/black"

        />

    <TextView
        android:id="@+id/productSubTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/productTitle"
        android:text = "Product Sub title"
        android:visibility="gone"
        />

    <TextView
        android:id="@+id/productQuantity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/productSubTitle"
        android:text = "200 kg"
        android:textColor="@color/gray"
        />

    </LinearLayout>

    <ImageView
        android:id="@+id/detailsIcon"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="8dp"
        android:layout_alignParentLeft="true"
        android:src="@drawable/info_circle"
        android:paddingLeft="@dimen/margin_16"
        />

    <LinearLayout
        android:id="@+id/priceLayout"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/cart_price_height"
        android:gravity="center"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:padding="4dp"
        android:layout_marginRight="8dp"
        >

        <TextView
            android:id="@+id/priceDollar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0."
            android:textStyle="bold"
            android:textSize="24sp"
            android:paddingLeft="4dp"
            android:textColor="@color/black"
            fontPath="fonts/SourceSansPro-Bold.ttf"/>

        <TextView
            android:id="@+id/priceCent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="00"
            android:layout_marginLeft="-6dp"
            android:layout_marginTop="-1dp"
            android:textStyle="bold"
            android:textSize="14sp"
            android:paddingRight="8dp"
            android:textColor="@color/black"
            fontPath="fonts/SourceSansPro-Bold.ttf"/>


    </LinearLayout>


    <LinearLayout
        android:id="@+id/priceLayout2"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/cart_price_height"
        android:gravity="center"
        android:layout_toLeftOf="@id/priceLayout"
        android:layout_alignParentBottom="true"
        android:padding="4dp"
        android:visibility="invisible"
        >

        <TextView
            android:id="@+id/priceDollar2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0."
            android:textStyle="bold"
            android:textSize="24sp"
            android:paddingLeft="4dp"
            android:textColor="@color/gray"
            fontPath="fonts/SourceSansPro-Bold.ttf"/>

        <TextView
            android:id="@+id/priceCent2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="00"
            android:layout_marginLeft="-6dp"
            android:textStyle="bold"
            android:textSize="14sp"
            android:paddingRight="8dp"
            android:textColor="@color/gray"
            fontPath="fonts/SourceSansPro-Bold.ttf"/>


    </LinearLayout>


</RelativeLayout>

这是错误日志。

10-05 18:03:50.688 28933-28933/nl.proshore.ekoplaza E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: nl.proshore.ekoplaza, PID: 28933
                                                                      java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView nl.proshore.ekoplaza.adapter.ProductsGridViewAdapter$ViewHolder.productTitle' on a null object reference
                                                                          at nl.proshore.ekoplaza.adapter.ProductsGridViewAdapter.getView(ProductsGridViewAdapter.java:102)
                                                                          at nl.proshore.ekoplaza.utils.SimpleSectionedGridAdapter.getView(SimpleSectionedGridAdapter.java:329)
                                                                          at android.widget.AbsListView.obtainView(AbsListView.java:2360)
                                                                          at android.widget.GridView.makeAndAddView(GridView.java:1440)
                                                                          at android.widget.GridView.makeRow(GridView.java:366)
                                                                          at android.widget.GridView.fillDown(GridView.java:307)
                                                                          at android.widget.GridView.fillGap(GridView.java:267)
                                                                          at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5191)
                                                                          at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3573)
                                                                          at android.widget.AbsListView.onTouchMove(AbsListView.java:3982)
                                                                          at android.widget.AbsListView.onTouchEvent(AbsListView.java:3807)
                                                                          at android.view.View.dispatchTouchEvent(View.java:9939)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2663)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2344)
                                                                          at dev.dworks.libs.astickyheader.ui.PinnedSectionGridView.dispatchTouchEvent(PinnedSectionGridView.java:487)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
                                                                          at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:411)
                                                                          at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1810)
                                                                          at android.app.Activity.dispatchTouchEvent(Activity.java:3061)
                                                                          at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
                                                                          at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
                                                                          at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:373)
                                                                          at android.view.View.dispatchPointerEvent(View.java:10159)
                                                                          at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4434)
                                                                          at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4302)
                                                                          at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
                                                                          at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
                                                                          at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
                                                                          at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3995)
                                                                          at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
                                                                          at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4052)
                                                                          at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
                                                                          at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
                                                                          at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
                                                                          at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
                                                                          at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
                                                                          at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6210)
android gridview
2个回答
0
投票

发生这种情况是因为您的视图为空。确保您的文本视图位于 products_grid_item_layout.xml 布局文件中,或者您的 viewHolder 为空。我将调试代码并发现问题。

还有一个提示,不要使用 Gridview,而要使用 Recyclerview。例如,参考这个链接


0
投票

用于向 ListView 或 GridView 添加粘性标题的库

 implementation 'com.github.DWorkS:AStickyHeader:9a40740d1a'

是最新版本,但目前似乎无法使用

请参阅文档

© www.soinside.com 2019 - 2024. All rights reserved.