gpt4 book ai didi

java - 如何处理android java中的bad_alloc

转载 作者:搜寻专家 更新时间:2023-11-01 03:47:49 26 4
gpt4 key购买 nike

我正在使用 ViewPager 来显示 fragment 。当我多次滑动它时,会出现以下错误:

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 上显示文本。
我试图从其他链接获得帮助,但未能成功。

最佳答案

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

为了简单起见,我使用的是 Picasso。我已经创建了一个示例项目,以防您需要引用。您需要在 gradle 依赖项中添加 compile 'com.squareup.picasso:picasso:2.5.2'

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) fragment 适配器

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

}
}

关于java - 如何处理android java中的bad_alloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39325554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com