我用来根据项目高度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;
}