使用view pager获取IndexoutofBounds异常

时间:2017-09-15 12:50:28

标签: java android android-viewpager android-imageview image-gallery

我是Android应用开发的新手,我正在按照本教程创建一个图库。 https://deepshikhapuri.wordpress.com/2017/03/20/get-all-images-from-gallery-in-android-programmatically/
它工作得很好但现在我想在视图寻呼机中显示图像,因为能够通过向左/向右滑动来显示图像。我正在使用适配器来做它但我想我做错了,因为它给了IndexoutOfBounds异常。任何人都可以帮助我吗?我是否在适配器中为图像提供了错误的路径?

TouchImageAdapter.java

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

class TouchImageAdapter extends PagerAdapter {
    Context context;
    String filename;
    ArrayList<Model_images> al_menu = new ArrayList<>();
    int position,int_position;

    @Override
    public int getCount() {
        return 1;
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        ImageView img = new ImageView(container.getContext());
        img.setImageDrawable(getImageFromSdCard(filename));
        container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        return img;
    }

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

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

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


    public Drawable getImageFromSdCard(String imageName) {
        Drawable d = null;
        try {
            String path = al_menu.get(int_position).getAl_imagepath().get(position)
                    + "/";
            Bitmap bitmap = BitmapFactory.decodeFile(path + "/" + imageName
                    + ".jpeg");
            d = new BitmapDrawable(context.getResources(),bitmap);
        } catch (IllegalArgumentException e) {
            // TODO: handle exception
        }
        return d;

    }
}

LOGCAT:

09-15 18:15:34.791 13080-13080/com.example.dell_1.Myapp3 E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.example.dell_1.Myapp3, PID: 13080
 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
 at java.util.ArrayList.get(ArrayList.java:411)
 at com.example.dell_1.myapp3.ImageViewer.TouchImageAdapter.getImageFromSdCard(TouchImageAdapter.java:53)
 at com.example.dell_1.myapp3.ImageViewer.TouchImageAdapter.instantiateItem(TouchImageAdapter.java:30)
 at com.example.dell_1.myapp3.ImageViewer.TouchImageAdapter.instantiateItem(TouchImageAdapter.java:16)
 at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
 at android.support.v4.view.ViewPager.populate(ViewPager.java:1182)
 at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
 at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
 at android.view.View.measure(View.java:19734)
 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6126)
 at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
 at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1117)
 at android.widget.LinearLayout.onMeasure(LinearLayout.java:642)
 at android.view.View.measure(View.java:19734)
 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6126)
 at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
 at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
 at android.view.View.measure(View.java:19734)
 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6126)
 at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
 at android.view.View.measure(View.java:19734)
 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6126)
 at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
 at android.view.View.measure(View.java:19734)
 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6126)
 at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
 at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
 at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
 at android.view.View.measure(View.java:19734)
 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6126)
 at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
 at com.android.internal.policy.DecorView.onMeasure(DecorView.java:687)
 at android.view.View.measure(View.java:19734)
 at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2303)
 at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1390)
 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1639)
 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1278)
 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6357)
 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
 at android.view.Choreographer.doCallbacks(Choreographer.java:683)
 at android.view.Choreographer.doFrame(Choreographer.java:619)
 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
 at android.os.Handler.handleCallback(Handler.java:751)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:154)
 at android.app.ActivityThread.main(ActivityThread.java:6077)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

2 个答案:

答案 0 :(得分:1)

您收到IndexOutOfBounds,因为您的位置字段未定义: 将代码调整为以下内容:

public Drawable getImageFromSdCard(String imageName,int position) {

img.setImageDrawable(getImageFromSdCard(filename,position));

这应解决这个问题

修改

根据评论,适配器的另一个问题是计数

像这样更改你的代码:

@Override
public int getCount() {
    return al_menu.getSize();
}

编辑2

别忘了填写你的清单 1-添加构造函数public

TouchImageAdapter(Context context,List<Model_images> alMenu){
    this.al_menu = alMenu;
    this.context = context;
}

2-当您在活动中调用构造函数时,请应用以下代码:

TouchImageAdapter myAdapter = new TouchImageAdapter(getActivity(),yourList);

答案 1 :(得分:0)

你没有初始化al_menu列表。 al_menu为空,因此您将获得indexoutofexception。即使您使用多个项目初始化列表,您的计数方法也会返回1,您将始终获得列表中的第一个。