我使用ViewPager创建了一个幻灯片,它运行正常,但它有时会崩溃我的应用程序。只有3个图片加载到其中,我使用一个Timer使其自动滚动,onPause我取消了计时器。
我真的不明白为什么它会不时崩溃,这可能是一些内存问题?图像非常小,我怎么能解决这个问题?
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);
}
}
主要活动:
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:二进制XML文件行#7:错误 膨胀班 android.view.LayoutInflater.createView(LayoutInflater.java:626)at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 在android.view.LayoutInflater.onCreateView(LayoutInflater.java:675) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700) 在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) 在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) 在 android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514) 在 android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:506) 在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) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)at dalvik.system.NativeStart.main(Native方法)引起: 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 更多引起:java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)at at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513) 在 android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889) 在android.content.res.Resources.loadDrawable(Resources.java:3436)at android.content.res.TypedArray.getDrawable(TypedArray.java:602)at at android.widget.ImageView。(ImageView.java:133)at android.widget.ImageView。(ImageView.java:123)... 25更多
答案 0 :(得分:0)
您可以使用Runnable
和Handler
来替换Timer
。并显示您的logcat
并尝试使用thiss method进行比例位图
答案 1 :(得分:0)
正如我所料,你通过加载太大/太多的图像来获得OutOfMemory异常。
引起:java.lang.OutOfMemoryError at android.graphics.BitmapFactory。
要避免这种情况,请阅读this article和this one,并在使用后忘记recycle()张图片。同时将图像放入drawable-nodpi
资源目录。