5

I am using a ViewPager to show fragments. When i swipe it multiple times it gives following error:

E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
--------- beginning of crash
A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 6900

I am showing and caching image (using this) as well as i am using TextView to show text on Fragment.
I tried to get help from other links but could not get succeed.

Viks
  • 1,510
  • 4
  • 22
  • 50
  • Are you running on real device or emulator? Probably you problem is related to this - http://stackoverflow.com/questions/15568475/android-emulator-error-stdbad-alloc – Amad Yus Sep 05 '16 at 07:30
  • I am using real device (Nexus 5) – Viks Sep 05 '16 at 07:31

1 Answers1

0

I tried to duplicate your issue on my side, however I'm not getting the error but the images are not loaded. But the files got cached in my internal storage. By the way, in your case, it is advisable to use Picasso or Universal Image Loader as those libraries will handle loading, caching and even error. This may not be your direct solution to your problem, but just in case if you're looking for alternative, you can try this solution.

For the sake of simplicity, I am using Picasso. I have created an example project just in case if you need reference. You need to add compile 'com.squareup.picasso:picasso:2.5.2' in your gradle dependency;

1) Fragment

public class FragmentImage extends Fragment {

private TextView imageName;
private ImageView image;

public static final String IMAGE_URL = "link";
public static final String POSITION = "position";

private String url = null;
private int position = 0;

public static FragmentImage newInstance(String link, int position) {
    // Required empty public constructor
    FragmentImage fragmentImage = new FragmentImage();
    Bundle bundle = new Bundle();
    bundle.putString(IMAGE_URL, link);
    bundle.putInt(POSITION, position);
    fragmentImage.setArguments(bundle);
    return fragmentImage;
}

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

    if(null != getArguments()){
        url = getArguments().getString(IMAGE_URL);
        position = getArguments().getInt(POSITION);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_fragment_image, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    image = (ImageView)view.findViewById(R.id.image);
    imageName = (TextView)view.findViewById(R.id.imageName);

    imageName.setText("Position "+position);
    Picasso.with(getActivity())
            .load(url)
            .placeholder(R.mipmap.ic_launcher)
            .error(R.mipmap.ic_launcher)
            .into(image);
}
}

2) FragmentAdapter

public class FragmentImagePager extends FragmentPagerAdapter {

private String[] imageUrls = {"https://www.ricoh.com/r_dc/caplio/r7/img/sample_04.jpg",
"http://i-cdn.phonearena.com/images/articles/47012-image/photo2.jpg",
"http://www.flooringvillage.co.uk/ekmps/shops/flooringvillage/images/request-a-sample--547-p.jpg",
"http://www.cameraegg.org/wp-content/uploads/2013/03/Canon-EOS-100D-Rebel-SL1-Sample-Image.jpg",
"http://imgsv.imaging.nikon.com/lineup/lens/singlefocal/wide/af-s_35mmf_14g/img/sample/sample4_l.jpg"};

public FragmentImagePager(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    return FragmentImage.newInstance(imageUrls[position], position);
}

@Override
public int getCount() {
    return imageUrls.length;
}
}

3) Activity

public class MainActivity extends AppCompatActivity{

private ViewPager fragmentList;
private FragmentImagePager fragmentImagePager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    fragmentList = (ViewPager)findViewById(R.id.fragmentList);
    fragmentImagePager = new FragmentImagePager(getSupportFragmentManager());
    fragmentList.setAdapter(fragmentImagePager);
    fragmentList.setOffscreenPageLimit(fragmentImagePager.getCount());

}
}
Dino
  • 7,779
  • 12
  • 46
  • 85
Amad Yus
  • 2,856
  • 1
  • 24
  • 32