当我尝试使用Universal Image Loader在gridview中设置图像时,我得到了一个nullpointer异常。崩溃发生的同时,第一个图像被下载,并且在应用程序崩溃之前只下载了一个图像。 (使用httpscoop查看)
修改: 这是错的。崩溃发生在第一个图像下载之前,因此可能会尝试测量不存在的图像。
这是我的网站
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/gridImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/gallery_grid_image_desc"/>
gridview布局只是一个gridview
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/galleryPicturesGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:verticalSpacing="@dimen/gallery_grid_spacing"
android:horizontalSpacing="@dimen/gallery_grid_spacing"
android:numColumns="3" />
我的getView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder = null;
Log.d(TAG, "getView " + position);
if (view == null) {
view = ((Activity) context).getLayoutInflater().inflate(
R.layout.gallery_grid_item, null);
holder = new ViewHolder();
holder.thumb = (ImageView) view.findViewById(R.id.gridImageView);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
Picture picture = mPictures.valueAt(position);
mImageLoader.displayImage(picture.getThumbUrl(), holder.thumb, mImageOptions);
return null;
}
ImageLoader配置
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext()).memoryCacheSize(2 * 1024 * 1024)
.discCache(new UnlimitedDiscCache(cacheDir))
.denyCacheImageMultipleSizesInMemory()
.offOutOfMemoryHandling()
.threadPoolSize(4)
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
.tasksProcessingOrder(QueueProcessingType.LIFO)
.imageDownloader(new UrlConnectionImageDownloader(this))
.build();
和选项
galleryThumbImageOptions = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.p1_cover)
.cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
和例外
01-18 10:56:15.434: E/AndroidRuntime(4089): FATAL EXCEPTION: main
01-18 10:56:15.434: E/AndroidRuntime(4089): java.lang.NullPointerException
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.widget.GridView.onMeasure(GridView.java:937)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.View.measure(View.java:8313)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.View.measure(View.java:8313)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.View.measure(View.java:8313)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.View.measure(View.java:8313)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.View.measure(View.java:8313)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.View.measure(View.java:8313)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.View.measure(View.java:8313)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.View.measure(View.java:8313)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.os.Looper.loop(Looper.java:123)
01-18 10:56:15.434: E/AndroidRuntime(4089): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-18 10:56:15.434: E/AndroidRuntime(4089): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 10:56:15.434: E/AndroidRuntime(4089): at java.lang.reflect.Method.invoke(Method.java:507)
01-18 10:56:15.434: E/AndroidRuntime(4089): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-18 10:56:15.434: E/AndroidRuntime(4089): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-18 10:56:15.434: E/AndroidRuntime(4089): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:19)
从null
返回getView()
是错误的,您应该返回刚刚膨胀或转换的行项目。