当我尝试使用通用图像加载器在网格视图中设置图像时,出现空指针异常。崩溃发生在下载第一个图像时,并且在应用程序崩溃之前只下载了一个图像。(通过 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)