如何在android java中处理bad_alloc

时间:2016-09-05 07:18:53

标签: java android android-fragments sigabrt bad-alloc

我使用ViewPager来显示片段。当我多次滑动它时会出现以下错误:

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

我正在展示和缓存图片(using this)以及我正在使用TextViewFragment上显示文字。
我试图从其他链接获得帮助,但无法取得成功。

1 个答案:

答案 0 :(得分:0)

我尝试在您身边复制您的问题,我没有收到错误但图片未加载。但文件已缓存在我的内部存储中。顺便说一句,在您的情况下,建议使用PicassoUniversal Image Loader,因为这些库将处理加载,缓存甚至错误。这可能不是您直接解决问题的方法,但如果您正在寻找替代方案,可以试试这个解决方案。

为了简单起见,我正在使用毕加索。我已经创建了一个示例项目以防万一你需要参考。您需要在gradle依赖项中添加compile 'com.squareup.picasso:picasso:2.5.2';

1)片段

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)活动

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());

}
}