0

我用来根据项目高度StaggeredGridView显示项目。GridView它在许多版本中运行良好。但是,最近我发现它在 android 5.1 中显示错误。我分享Logcat如下。请检查一下帮助我解决这个问题。

06-03 15:17:22.266: E/AndroidRuntime(29840): java.lang.NullPointerException: Attempt to invoke interface method 'int android.widget.ListAdapter.getCount()' on a null object reference
06-03 16:03:25.229: E/AndroidRuntime(25195):    at com.etsy.android.grid.ExtendableListView.getLastVisiblePosition(ExtendableListView.java:1824)
06-03 16:03:25.229: E/AndroidRuntime(25195):    at android.widget.AdapterView.onInitializeAccessibilityEvent(AdapterView.java:978)
06-03 16:03:25.229: E/AndroidRuntime(25195):    at android.widget.AbsListView.onInitializeAccessibilityEvent(AbsListView.java:1490)
06-03 16:03:25.229: E/AndroidRuntime(25195):    at android.view.View.sendAccessibilityEventUncheckedInternal(View.java:5317)
06-03 16:03:25.229: E/AndroidRuntime(25195):    at android.view.View.sendAccessibilityEventUnchecked(View.java:5304)
06-03 16:03:25.229: E/AndroidRuntime(25195):    at android.view.View$SendViewStateChangedAccessibilityEvent.run(View.java:21039)
06-03 16:03:25.229: E/AndroidRuntime(25195):    at android.os.Handler.handleCallback(Handler.java:739)
06-03 16:03:25.229: E/AndroidRuntime(25195):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-03 16:03:25.229: E/AndroidRuntime(25195):    at android.os.Looper.loop(Looper.java:135)
06-03 16:03:25.229: E/AndroidRuntime(25195):    at android.app.ActivityThread.main(ActivityThread.java:5343)
06-03 16:03:25.229: E/AndroidRuntime(25195):    at java.lang.reflect.Method.invoke(Native Method)
06-03 16:03:25.229: E/AndroidRuntime(25195):    at java.lang.reflect.Method.invoke(Method.java:372)
06-03 16:03:25.229: E/AndroidRuntime(25195):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
06-03 16:03:25.229: E/AndroidRuntime(25195):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

我还可以看到如下警告消息:

06-03 15:52:05.804: W/art(21986): Before Android 4.1, method void com.etsy.android.grid.ExtendableListView.reportScrollStateChange(int) would have incorrectly overridden the package-private method in android.widget.AbsListView

我的代码:

Mainactivity.java

productGridviewAdapter = new ProductGridviewAdapter(getActivity(), productList,);
gvProducts.setAdapter(productGridviewAdapter);

ProductGridviewAdapter.java

public ProductGridviewAdapter(Activity a,
            ArrayList<HashMap<String, String>> arlData) {

        activity = a;
        data = arlData;

        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        WEBSITE_URL = activity.getResources().getString(R.string.website_url);

        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder {
        ImageView imgPhoto;
        TextView tvName;
        TextView tvType;
        TextView tvDiscountedPrice;
    }

    public View getView(int position, final View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;

        products = new HashMap<String, String>();
        products = data.get(position);

        View vi = convertView;
        if (convertView == null) {
            viewHolder = new ViewHolder();

            if (products.get("MRP").equals("0")) {
                vi = inflater.inflate(R.layout.package_product_item, parent,
                        false);

                viewHolder.tvType = (TextView) vi.findViewById(R.id.tvType);
            } else {
                vi = inflater.inflate(R.layout.product_item, parent, false);

                viewHolder.tvDiscountedPrice = (TextView) vi
                        .findViewById(R.id.tvDiscountedPrice);
                viewHolder.tvMRP = (TextView) vi.findViewById(R.id.tvMRP);
                viewHolder.tvDiscount = (TextView) vi
                        .findViewById(R.id.tvDiscount);
            }

            viewHolder.imgPhoto = (ImageView) vi.findViewById(R.id.imgPhoto);
            viewHolder.tvName = (TextView) vi.findViewById(R.id.tvName);
            viewHolder.tvOutofStock = (TextView) vi
                    .findViewById(R.id.tvOutofStock);

            vi.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        imageLoader.DisplayImage(WEBSITE_URL + products.get("Photo"),
                viewHolder.imgPhoto);
        viewHolder.tvName.setText(products.get("ProductName"));

        viewHolder.tvDiscountedPrice.setText(activity.getResources()
                    .getString(R.string.rs)
                    + " "
                    + products.get("DiscountedPrice"));


        return vi;
    }
4

1 回答 1

1

我没有看到你的整个代码,但我可以说在某些情况下你的 ProductGridviewAdapter 对象有一个空列表。您想查看您的代码并找出可能发生的时间/地点。还要注意在初始化列表之前构造对象的竞争条件。这可能会导致您正在观察的随机性。我不喜欢在代码中留下未知条件,但是如果出于某种原因您的列表为空,您可能希望将其视为空。此外,您可能希望将其初始化为一个空列表以开始。以下更改应该消除错误,但不一定是列表为空的根本原因。我强烈建议您尝试弄清楚为什么会发生这种情况,因为这可能表明存在另一个(有时更严重)问题。

public ProductGridviewAdapter(Activity a,
            ArrayList<HashMap<String, String>> arlData) {

        activity = a;
        //check for null here as a safety net
        if (arlData != null)
            data = arlData;
        else
            data = new ArrayList<HashMap<String, String>>();
        ...
    //if somehow data is set to null at some point, treat it as empty list
    public int getCount() {
        return data==null?0:data.size();
        //alternatively you might want to initialize data in this case
    }

祝你好运!

于 2016-06-10T18:36:13.180 回答