Reputation: 3305
I have created a slideshow using ViewPager, it works ok, but it crashes my app sometimes. There are only 3 pics loaded into it, I use a Timer to make it scroll automatically, onPause I cancel the timer.
I don't really understand why does it crash from time to time, It may be some memory issue? The images are quite small, how could I fix this?
CustomPagerAdapter:
public class CustomPagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
int[] mResources;
private ImageView imageView;
public CustomPagerAdapter(Context context, int[] mResources) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mResources = mResources;
}
@Override
public int getCount() {
return mResources.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
try {
imageView.setImageResource(mResources[position]);
container.addView(itemView);
} catch (Exception e) {
}
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
Main Activity:
private CustomPagerAdapter mCustomPagerAdapter;
private ViewPager mViewPager;
private Timer swipeTimer;
protected int currentPage;
int[] mResources = {
R.drawable.homeslide1,
R.drawable.homeslide3,
R.drawable.homeslide2
};
private Editor editor;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.home, container, false);
editor = getActivity().getSharedPreferences("orderStatus", Context.MODE_PRIVATE).edit();
mCustomPagerAdapter = new CustomPagerAdapter(getActivity(), mResources);
mViewPager = (ViewPager) v.findViewById(R.id.imageSliderHome);
mViewPager.setAdapter(mCustomPagerAdapter);
mViewPager.beginFakeDrag();
}
}
@Override
public void onResume() {
super.onResume();
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (currentPage == mCustomPagerAdapter.getCount()) {
currentPage = 0;
}
mViewPager.setCurrentItem(currentPage++, true);
}
});
}
}, 1000, 5000);
}
public void onPause() {
super.onPause();
swipeTimer.cancel();
}
pager_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/splash_bg1" />
</LinearLayout>
logcat:
android.view.InflateException: Binary XML file line #7: Error inflating class at android.view.LayoutInflater.createView(LayoutInflater.java:626) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700) at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) at android.view.LayoutInflater.inflate(LayoutInflater.java:498) at android.view.LayoutInflater.inflate(LayoutInflater.java:398) at foto.studio.CustomPagerAdapter.instantiateItem(CustomPagerAdapter.java:38) at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837) at android.support.v4.view.ViewPager.populate(ViewPager.java:1053) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:555) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514) at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:506) at foto.studio.HomeActivity$7$1.run(HomeActivity.java:146) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at android.view.LayoutInflater.createView(LayoutInflater.java:600) ... 22 more Caused by: java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889) at android.content.res.Resources.loadDrawable(Resources.java:3436) at android.content.res.TypedArray.getDrawable(TypedArray.java:602) at android.widget.ImageView.(ImageView.java:133) at android.widget.ImageView.(ImageView.java:123) ... 25 more
Upvotes: 2
Views: 925
Reputation: 10871
As I expected, you get the OutOfMemory exception by loading too large/too many images.
Caused by: java.lang.OutOfMemoryError at android.graphics.BitmapFactory.
To avoid it, read this article and this one, and don't forget to recycle() images after using them. Also put the images into drawable-nodpi
resource dir.
Upvotes: 0
Reputation: 1266
U can use Runnable
and Handler
to replace Timer
. And show your logcat
And try thiss method for scale bitmap
Upvotes: 0