0

我面臨這三個錯誤,我的應用程序崩潰。android.view.InflateException,java.lang.reflect.InvocationTargetException,java.lang.OutOfMemoryError

Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class <unknown> 
Caused by: java.lang.reflect.InvocationTargetException 
Caused by: java.lang.OutOfMemoryError 

本網站上的一些人建議圖像文件可能太大,導致內存不足錯誤,從而導致另外兩個錯誤。我使用的圖像大約是50KB,太小了,我甚至嘗試了大約4KB的空白圖像,但問題仍然存在。下面是我的代碼,誰能幫我解決這個問題。

public class occasions extends Activity { 

TextView linkToPageTextView; 
String LinkToPageTxt = "Click to visit Page"; 

ImageView headerImageView; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gridview_options); 

    GridView gridView = (GridView)findViewById(R.id.gridview); 
    gridView.setAdapter(new MyAdapter(this)); 
    gridView.setNumColumns(2); 

    headerImageView = (ImageView) findViewById(R.id.header_img); 
    headerImageView.setBackgroundResource(R.drawable.stock); 

    linkToPageTextView = (TextView) findViewById(R.id.link_to_page_text); 
    linkToPageTextView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      // Do something 
     } 
    }); 
} 

private class MyAdapter extends BaseAdapter 
{ 
    private List<Item> items = new ArrayList<Item>(); 
    private LayoutInflater inflater; 

    ImageView option_picture; 
    TextView option_name; 

    public MyAdapter(Context context) 
    { 
     inflater = LayoutInflater.from(context); 

     items.add(new Item("A", R.drawable.ps_tousled)); 
     items.add(new Item("B", R.drawable.ps_googiesdiner)); 
     items.add(new Item("C", R.drawable.ps_drugstore)); 
     items.add(new Item("D", R.drawable.ps_misc));  
    } 

    @Override 
    public int getCount() { 
     return items.size(); 
    } 

    @Override 
    public Object getItem(int i) 
    { 
     return items.get(i); 
    } 

    @Override 
    public long getItemId(int i) 
    { 
     return items.get(i).drawableId; 
    } 

    @Override 
    public View getView(final int i, View view, ViewGroup viewGroup) 
    { 
     View v = view; 

     if(v == null) 
     { 
      v = inflater.inflate(R.layout.gridview_item, viewGroup, false); 
      v.setTag(R.id.picture, v.findViewById(R.id.picture)); 
      v.setTag(R.id.option_text, v.findViewById(R.id.option_text)); 
     } 

     option_picture = (ImageView)v.getTag(R.id.picture); 
     option_name = (TextView)v.getTag(R.id.option_text); 

     Item item = (Item)getItem(i); 

     option_picture.setImageResource(item.drawableId); 
     option_name.setText(item.name); 

     option_name.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent album_Activity = new Intent(occasions.this, 
         GridViewActivity.class); 

       if(i == 0) 
        { 
         album_Activity.putExtra("album_ID", "Series"); 
       } 
       else if(i == 1) 
       { 
        album_Activity.putExtra("album_ID", "Christmas"); 
       } 
       else if(i == 2) 
       { 
        album_Activity.putExtra("album_ID", "NY"); 

       } 
       else if(i == 3) 
       { 

        album_Activity.putExtra("album_ID", "Misc"); 
       } 

       album_Activity.putExtra("sub_ID", 0); 
       startActivity(album_Activity); 
      } 
     }); 

     return v; 
    } 
} 

private class Item 
{ 
    final String name; 
    final int drawableId; 

    Item(String name, int drawableId) 
    { 
     this.name = name; 
     this.drawableId = drawableId; 
    } 
} 
} 

和XML文件如下:

<?xml version="1.0" encoding="utf-8"?> 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#fff" 
android:padding="5dp"> 

<com.jbd.abc.SquareImageView 
    android:id="@+id/picture" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scaleType="centerCrop" 
    /> 
<TextView 
    android:id="@+id/option_text" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingTop="15dp" 
    android:paddingBottom="15dp" 
    android:layout_gravity="bottom" 
    android:textColor="@android:color/white" 
    android:background="#55000000" 
    android:textStyle="bold" 
    android:textSize="22dip" 
    android:textIsSelectable="true" 
    /> 
</FrameLayout> 
+0

您是否設法解決它? – XcodeNOOB

+0

@XcodeNOOB是的,我做到了。請在下面的帖子中查看我的回答,因爲我無法在此評論中正確添加代碼。 – Heidi

回答

1
public static Bitmap decodeFile(int filePath, int WIDTH, int HIGHT) 
{ 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    //BitmapFactory.decodeStream(new FileInputStream(f), null, o); 
    BitmapFactory.decodeStream(_context.getResources().openRawResource(filePath), 
      null,o); 

    final int REQUIRED_WIDTH = WIDTH; 
    final int REQUIRED_HIGHT = HIGHT; 
    int scale = 1; 
    while (o.outWidth/scale/2 >= REQUIRED_WIDTH 
      && o.outHeight/scale/2 >= REQUIRED_HIGHT) 
     scale *= 2; 

    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    //return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    return BitmapFactory.decodeStream(_context.getResources().openRawResource(filePath), 
      null, o2); 
} 

以及將所述行到清單文件。

android:largeHeap="true" 
相關問題