我正在使用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;
}
答案 0 :(得分:1)
我没有看到你的整个代码,但我可以说在某些情况下你的ProductGridviewAdapter对象有一个空列表。您希望查看代码并查找可能发生的时间/地点。在列表初始化之前,还要在构建对象的地方密切关注竞争条件。这可能会导致您观察到的随机性。我不喜欢在代码中留下未知条件,但如果由于任何原因您的列表为空,您可能希望将其视为空。此外,您可能希望将其初始化为空列表以开始。以下更改应该消除错误,但不一定是列表的基本原因为null。我强烈建议你试着找出发生这种情况的原因,因为它可能表明另一个(有时更严重的)问题。
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
}
祝你好运!