android.view.InflateException、java.lang.reflect.InvocationTargetException、java.lang.OutOfMemoryError
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>
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"
我遇到了这三个导致我的应用程序崩溃的错误。
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>
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"