我使用https://github.com/etsy/AndroidStaggeredGrid为我的应用程序实现StaggeredGridView。当我尝试初始化DynamicHeightImageView时,我得到一个NullPointerException。
我无法弄清楚出了什么问题。
这是我的代码: -
row_staggered.xml
<com.etsy.android.grid.util.DynamicHeightImageView
android:id="@+id/imgViewDynamic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
StaggeredAdapter.java
static class LazyViewHolder
{
DynamicHeightImageView thumb_image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LazyViewHolder viewHolder;
if(convertView==null)
convertView = inflater.inflate(R.layout.row_staggered,null);
viewHolder = new LazyViewHolder();
viewHolder.thumb_image=(DynamicHeightImageView)convertView.findViewById(R.id.imgViewDynamic); // thumb image
imageLoader.DisplayImage(bitmapUrl, viewHolder.thumb_image); //Error here Line 215
}
ExtendableListView.java
/**
* Get a view and have it show the data associated with the specified
* position. This is called when we have already discovered that the view is
* not available for reuse in the recycle bin. The only choices left are
* converting an old view or making a new one.
*
* @param position The position to display
* @param isScrap Array of at least 1 boolean, the first entry will become true if
* the returned view was taken from the scrap heap, false if otherwise.
* @return A view displaying the data associated with the specified position
*/
private View obtainView(int position, boolean[] isScrap) {
isScrap[0] = false;
View scrapView;
scrapView = mRecycleBin.getScrapView(position);
View child;
if (scrapView != null) {
if (DBG) Log.d(TAG, "getView from scrap position:" + position);
child = mAdapter.getView(position, scrapView, this);
if (child != scrapView) {
mRecycleBin.addScrapView(scrapView, position);
}
else {
isScrap[0] = true;
}
}
else {
if (DBG) Log.d(TAG, "getView position:" + position);
child = mAdapter.getView(position, null, this); //Error here
}
return child;
}
/**
* Gets a view either a new view an unused view?? or a recycled view and adds it to our children
*/
private View makeAndAddView(int position, int y, boolean flowDown, boolean selected) {
View child;
onChildCreated(position, flowDown);
if (!mDataChanged) {
// Try to use an existing view for this position
child = mRecycleBin.getActiveView(position);
if (child != null) {
// Found it -- we're using an existing child
// This just needs to be positioned
setupChild(child, position, y, flowDown, selected, true);
return child;
}
}
// Make a new view for this position, or convert an unused view if possible
child = obtainView(position, mIsScrap); //ERROR HERE
// This needs to be positioned and measured
setupChild(child, position, y, flowDown, selected, mIsScrap[0]);
return child;
}
private View fillDown(int pos, int nextTop) {
if (DBG) Log.d(TAG, "fillDown - pos:" + pos + " nextTop:" + nextTop);
View selectedView = null;
int end = getHeight();
if (mClipToPadding) {
end -= getListPaddingBottom();
}
while ((nextTop < end || hasSpaceDown()) && pos < mItemCount) {
// TODO : add selection support
makeAndAddView(pos, nextTop, true, false);
pos++;
nextTop = getNextChildDownsTop(pos); // = child.getBottom();
}
return selectedView;
}
/**
* Fills the list from top to bottom, starting with mFirstPosition
*/
private View fillFromTop(int nextTop) {
mFirstPosition = Math.min(mFirstPosition, mItemCount - 1);
if (mFirstPosition < 0) {
mFirstPosition = 0;
}
return fillDown(mFirstPosition, nextTop);
}
switch (mLayoutMode) {
case LAYOUT_FORCE_TOP: {
mFirstPosition = 0;
resetToTop();
adjustViewsUpOrDown();
fillFromTop(childrenTop);
adjustViewsUpOrDown();
break;
}
我收到错误: -
06-10 05:05:23.389: E/AndroidRuntime(2178): java.lang.NullPointerException
06-10 05:05:23.389: E/AndroidRuntime(2178): at Utility.StaggeredAdapter.getView(StaggeredAdapter.java:215)
06-10 05:05:23.389: E/AndroidRuntime(2178): at com.etsy.android.grid.ExtendableListView.obtainView(ExtendableListView.java:1578)
06-10 05:05:23.389: E/AndroidRuntime(2178): at com.etsy.android.grid.ExtendableListView.makeAndAddView(ExtendableListView.java:1402)
06-10 05:05:23.389: E/AndroidRuntime(2178): at com.etsy.android.grid.ExtendableListView.fillDown(ExtendableListView.java:1288)
06-10 05:05:23.389: E/AndroidRuntime(2178): at com.etsy.android.grid.ExtendableListView.fillFromTop(ExtendableListView.java:1336)
06-10 05:05:23.389: E/AndroidRuntime(2178): at com.etsy.android.grid.ExtendableListView.layoutChildren(ExtendableListView.java:587)
答案 0 :(得分:0)
在** StaggeredAdapter.java
**类
ImageLoader imageloader = new ImageLoader();
// defining value to imageLoader