2012-05-16 80 views
0

我在我的應用程序中實現了一個使用ViewPager的活動,並使用PagerAdapter顯示4個圖像。您只能看到一個圖像,並顯示下一個您必須刷卡。 圖像的大小不是很大,我不知道爲什麼,但發生錯誤「java.lang.OutOfMemoryError位圖大小超過虛擬機預算pageradapter」。如何處理java.lang.OutOfMemoryError位圖大小超過虛擬機預算pageradapter?

這是我的代碼:

public class InfoActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.information); 

     MyPagerAdapter adapter = new MyPagerAdapter(); 
     ViewPager myPager = (ViewPager) findViewById(R.id.mypanelpager); 
     myPager.setAdapter(adapter); 
     myPager.setCurrentItem(0); 
    } 

    private class MyPagerAdapter extends PagerAdapter { 

     public int getCount() { 
      return 4; 
     } 

     public Object instantiateItem(View collection, int position) { 

      LayoutInflater inflater = (LayoutInflater) collection.getContext() 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      int resId = 0; 
      switch (position) { 
      case 0: 
       resId = R.layout.farleft; 
       break; 
      case 1: 
       resId = R.layout.left; 
       break; 
      case 2: 
       resId = R.layout.middle; 
       break; 
      case 3: 
       resId = R.layout.right; 
       break; 
      } 

      View view = inflater.inflate(resId, null); 

      ((ViewPager) collection).addView(view, 0); 

      return view; 
     } 

     @Override 
     public void destroyItem(View arg0, int arg1, Object arg2) { 
      ((ViewPager) arg0).removeView((View) arg2); 
     } 

     @Override 
     public void finishUpdate(View arg0) { 

     } 

     @Override 
     public boolean isViewFromObject(View arg0, Object arg1) { 
      return arg0 == ((View) arg1); 

     } 

     @Override 
     public void restoreState(Parcelable arg0, ClassLoader arg1) { 

     } 

     @Override 
     public Parcelable saveState() { 
      return null; 
     } 

     @Override 
     public void startUpdate(View arg0) { 
     } 

    } 

} 

的R.layout.right是:

<?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/imageView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:adjustViewBounds="true" 
     android:scaleType="fitXY" 
     android:src="@drawable/info4" 
     android:contentDescription="@string/fourthInfoImage"> 
    </ImageView> 

</LinearLayout> 

,其餘的是除了源圖像等號。 我該如何糾正這個錯誤?關於這個錯誤有幾個帖子,但我沒有找到解決我的問題的那個。我相信「destroyItem」功能不叫..

謝謝

P.S. 其實我遵循http://mobile.tutsplus.com

回答

相關問題