Android ViewPager + ImageLoader

时间:2012-12-14 06:21:32

标签: android android-logcat universal-image-loader

我使用ImageLoader库创建了一个ViewPager,我在这段代码中有错误,我不明白Log中他想要的东西!在主布局中,我在adapterLayout中只有ViewPager,我有FrameLayout和ImageView

public class ImageGallery extends Activity {

    ViewPager pager;

    String[] BigImages;
    String[] Authors;
    String[] Dates;
    String[] Descriptions;

    DisplayImageOptions options;

    @Override
    protected void onCreate(Bundle savedInstanceState){     
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        String[] imageUrls = bundle.getStringArray("big_images");
        int pagerPosition = bundle.getInt("position");

        options = new DisplayImageOptions.Builder()
            .showImageForEmptyUri(R.drawable.ic_contact_picture_2)
            .resetViewBeforeLoading()
            .cacheOnDisc()
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .displayer(new FadeInBitmapDisplayer(300))
            .build();


        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new GalleryImage(ImageGallery.this, imageUrls));
        pager.setCurrentItem(pagerPosition);

    }

class GalleryImage extends PagerAdapter{

    private String[] images;
    private LayoutInflater inflater;
    ImageLoader imageLoader;

    GalleryImage(Context context, String[] images) {
        this.images = images;
        inflater = getLayoutInflater();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));

    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }
    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object instantiateItem(View view, int position) {
        final View imageLayout = inflater.inflate(R.layout.item_gallery, null);
        final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);

        imageLoader.displayImage(images[position], imageView, options);


        ((ViewPager) view).addView(imageLayout, 0);
        return imageLayout;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

}}

日志

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gazetaimage/com.gazetaimage.ImageGallery}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
    at android.app.ActivityThread.access$600(ActivityThread.java:150)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5193)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at com.gazetaimage.ImageGallery.onCreate(ImageGallery.java:49)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
    ... 11 more

1 个答案:

答案 0 :(得分:0)

您似乎忘了添加第setContentView(R.layout.yourxml);

必须在调用此ViewPager pager = (ViewPager) findViewById(R.id.pager);之前完成。如果你不这样做,Android不知道在哪里寻找资源,显然它会返回null,这就是你得到 NullPointerException 的原因。