こんにちは、ViewPager に問題があり、ViewPager の使用中に XHDPI デバイスで OOM を取得しました。MDPI および HDPI デバイスで完全に動作します。
これが私の丸太猫です:
12-27 17:06:11.082: E/dalvikvm-heap(10480): 3936000-byte external allocation too large for this process.
12-27 17:06:11.082: E/GraphicsJNI(10480): VM won't let us allocate 3936000 bytes
12-27 17:06:11.102: E/AndroidRuntime(10480): FATAL EXCEPTION: main
12-27 17:06:11.102: E/AndroidRuntime(10480): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:359)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:382)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:408)
12-27 17:06:11.102: E/AndroidRuntime(10480): at com.phonethics.madhuridixitnene.MainImageFragment.bitmap(MainImageFragment.java:167)
12-27 17:06:11.102: E/AndroidRuntime(10480): at com.phonethics.madhuridixitnene.MainImageFragment.onCreateView(MainImageFragment.java:125)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:523)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:495)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:487)
12-27 17:06:11.102: E/AndroidRuntime(10480): at com.phonethics.madhuridixitnene.MadhuriHomeScreen$PageListener$5.run(MadhuriHomeScreen.java:666)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.os.Handler.handleCallback(Handler.java:587)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.os.Handler.dispatchMessage(Handler.java:92)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.os.Looper.loop(Looper.java:138)
12-27 17:06:11.102: E/AndroidRuntime(10480): at android.app.ActivityThread.main(ActivityThread.java:3701)
12-27 17:06:11.102: E/AndroidRuntime(10480): at java.lang.reflect.Method.invokeNative(Native Method)
12-27 17:06:11.102: E/AndroidRuntime(10480): at java.lang.reflect.Method.invoke(Method.java:507)
12-27 17:06:11.102: E/AndroidRuntime(10480): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
12-27 17:06:11.102: E/AndroidRuntime(10480): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
12-27 17:06:11.102: E/AndroidRuntime(10480): at dalvik.system.NativeStart.main(Native Method)
ビットマップのリサイクルを試みましたが、イベントは機能しませんでした。
私は約 22 の 1280X800 解像度の画像を持っています。しかし、ページャーでそれらを使用すると、3 回または 4 回スワイプした後にクラッシュします。
ここに私のフラグメントクラスがあります:
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
public class MainImageFragment extends Fragment{
Context context;
String imgname;
String animate;
int tagPosition;
Bitmap bm;
public MainImageFragment()
{
}
public MainImageFragment(Context context,String imgname,String animate,int position)
{
this.context=context;
this.imgname=imgname;
this.animate=animate;
tagPosition=position;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(savedInstanceState!=null)
{
imgname=savedInstanceState.getString("imgName");
animate=savedInstanceState.getString("animate");
context=getActivity().getApplicationContext();
}
View view=(View)inflater.inflate(R.layout.mainimagelayout,null);
try
{
ImageView imgView=(ImageView)view.findViewById(R.id.mainImage);
imgView.setTag(tagPosition);
bm=bitmap(imgname);
imgView.setImageBitmap(bm);
// imgView.setImageDrawable(context.getResources().getDrawable(context.getResources().getIdentifier("drawable/"+imgname, "drawable",context.getPackageName())));
if(animate.equalsIgnoreCase("1"))
{
/*Animation anim=AnimationUtils.loadAnimation(context, R.anim.fade_in);
anim.setFillAfter(true);
imgView.startAnimation(anim);*/
imgView.setVisibility(View.INVISIBLE);
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
if(savedInstanceState!=null)
{
imgname=savedInstanceState.getString("imgName");
animate=savedInstanceState.getString("animate");
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putString("imgName", imgname);
outState.putString("animate", animate);
context=getActivity().getApplicationContext();
}
private Bitmap bitmap(String name)
{
final Bitmap bitmap=BitmapFactory.decodeResource(getResources(), getResources().getIdentifier(name, "drawable", getActivity().getPackageName()));
return bitmap;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
bm.recycle();
super.onDestroy();
}
}
そして、ここに私のアダプタークラスがあります:
public class MainAdapter extends FragmentStatePagerAdapter
{
Context context;
ArrayList<String>imgname=null;
ArrayList<String>animate;
public MainAdapter(FragmentManager fm,Context context,ArrayList<String>imgname,ArrayList<String>animate) {
super(fm);
// TODO Auto-generated constructor stub
this.context=context;
this.imgname=imgname;
this.animate=animate;
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
return new MainImageFragment(context, imgname.get(position),animate.get(position),position);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imgname.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();
super.destroyItem(container, position, object);
}
}