setAdapter()崩溃应用程序

时间:2016-01-02 07:00:31

标签: java android

当我设置我的ArrayList后,在我的构造函数中调用 .notifyDataSetChanged()时,我的应用程序仍然崩溃,就像我在调用 .setAdapter 之前调用它时一样,它已经困扰了我几个小时,会喜欢任何帮助,谢谢!

public class CustomSwipeAdapter extends PagerAdapter {

private ArrayList<String> image_resources = new ArrayList<>();
private Context mContext;
private LayoutInflater mLayoutInflater;
private ImageLoader mImageLoader;

public CustomSwipeAdapter(Context context, ArrayList<String> images){
    this.mContext = context;
    this.image_resources = images;
}

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

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    mLayoutInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
    View item_view = mLayoutInflater.inflate(R.layout.swipe_layout, container, false);

    final ImageView imageView = (ImageView) item_view.findViewById(R.id.imageView);

    if (image_resources.get(position) != null || !image_resources.get(position).isEmpty()) {

        mImageLoader.get(image_resources.get(position), new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                imageView.setImageBitmap(response.getBitmap());
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                imageView.setBackgroundColor(Color.CYAN);
            }
        });
    }

    container.addView(item_view);

    return item_view;
}

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

我在哪里设置我的适配器(在我的活动&#39; onCreate()中),它是最基本的活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page);
    mViewPager = (ViewPager) findViewById(R.id.view_pager);

    mVolleySingleton = VolleySingleton.getInstance();
    mRequestQueue = mVolleySingleton.getRequestQueue();
    sendAPIRequest();

    //after you get the images
    mCustomSwipeAdapter = new CustomSwipeAdapter(this, images);
    mViewPager.setAdapter(mCustomSwipeAdapter);

}

堆栈追踪:

  java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 0, found: 4 Pager id: sobmad.com.gamingreminder:id/view_pager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class sobmad.com.gamingreminder.GamePage.CustomSwipeAdapter
        at android.support.v4.view.ViewPager.populate(ViewPager.java:967)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
        at android.view.View.measure(View.java:15848)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
        at android.view.Choreographer.doCallbacks(Choreographer.java:562)
        at android.view.Choreographer.doFrame(Choreographer.java:532)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

mCustomSwipeAdapter = new CustomSwipeAdapter(this, images);
mViewPager.setAdapter(mCustomSwipeAdapter);

mCustomSwipeAdapter.notifyDataSetChange() //Add this line in your activity