Android刷新图片在viewpager中

时间:2013-05-03 05:41:24

标签: android android-viewpager

我正在开发一个使用ViewPager显示图像的应用程序。在这个应用程序中,它将有5页,每页显示1个图像。我有Button可以刷新所有5页(意味着所有图像都不同)。这是我的代码:

ImageAdapter.java

public class ImageAdapter extends PagerAdapter {
    private Context context;
    private List<Integer> todayImage;

    ImageAdapter(Context context, List<Integer> todayImage) {
        this.context = context;
        this.todayImage = todayImage;
    }

    public void setImage(List<Integer> todayImage) {
        this.todayImage = todayImage;
    }

    @Override
    public int getCount() {
        return todayImage.size();
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        File imgFile = new File("data/data/com.project.project/"
                + todayImage.get(position) + ".jpg");
        Bitmap shuffleBit = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        imageView.setImageBitmap(shuffleBit);
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((ImageView) object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public void destroyItem(View collection, int position, Object view) {
        ((ViewPager) collection).removeView((View) view);
    }

}

这是我用来续订图像的Button

shuffle.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        adapter.setImage(newImage);
});

这是logcat:

05-03 01:17:50.793: E/AndroidRuntime(716): FATAL EXCEPTION: main
05-03 01:17:50.793: E/AndroidRuntime(716): java.lang.NullPointerException
05-03 01:17:50.793: E/AndroidRuntime(716):  at com.example.project.Main$1.onClick(Main.java:201)
05-03 01:17:50.793: E/AndroidRuntime(716):  at android.view.View.performClick(View.java:4084)
05-03 01:17:50.793: E/AndroidRuntime(716):  at android.view.View$PerformClick.run(View.java:16966)
05-03 01:17:50.793: E/AndroidRuntime(716):  at android.os.Handler.handleCallback(Handler.java:615)
05-03 01:17:50.793: E/AndroidRuntime(716):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-03 01:17:50.793: E/AndroidRuntime(716):  at android.os.Looper.loop(Looper.java:137)
05-03 01:17:50.793: E/AndroidRuntime(716):  at android.app.ActivityThread.main(ActivityThread.java:4745)
05-03 01:17:50.793: E/AndroidRuntime(716):  at java.lang.reflect.Method.invokeNative(Native Method)
05-03 01:17:50.793: E/AndroidRuntime(716):  at java.lang.reflect.Method.invoke(Method.java:511)
05-03 01:17:50.793: E/AndroidRuntime(716):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-03 01:17:50.793: E/AndroidRuntime(716):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-03 01:17:50.793: E/AndroidRuntime(716):  at dalvik.system.NativeStart.main(Native Method)

当我按下按钮时应用程序崩溃,任何人都可以帮我解决问题吗?我使用settag()找到了一些答案,但我不知道该怎么做。

0 个答案:

没有答案