我在我的活动“GameActivity”中使用了很多图像(我做了imageView.setImageDrawable(AnimationDrawable),其中AnimationDrawable有超过16个,我在第二个活动中没有使用的图像“ShareActivity”
然而出现此错误
02-13 12:36:47.943: E/AndroidRuntime(14790): FATAL EXCEPTION: main
02-13 12:36:47.943: E/AndroidRuntime(14790): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kersplatt/com.example.kersplatt.ShareActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class <unknown>
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.os.Looper.loop(Looper.java:130)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-13 12:36:47.943: E/AndroidRuntime(14790): at java.lang.reflect.Method.invokeNative(Native Method)
02-13 12:36:47.943: E/AndroidRuntime(14790): at java.lang.reflect.Method.invoke(Method.java:507)
02-13 12:36:47.943: E/AndroidRuntime(14790): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
02-13 12:36:47.943: E/AndroidRuntime(14790): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
02-13 12:36:47.943: E/AndroidRuntime(14790): at dalvik.system.NativeStart.main(Native Method)
02-13 12:36:47.943: E/AndroidRuntime(14790): Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class <unknown>
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
02-13 12:36:47.943: E/AndroidRuntime(14790): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-13 12:36:47.943: E/AndroidRuntime(14790): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.app.Activity.setContentView(Activity.java:1657)
02-13 12:36:47.943: E/AndroidRuntime(14790): at com.example.kersplatt.ShareActivity.onCreate(ShareActivity.java:65)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-13 12:36:47.943: E/AndroidRuntime(14790): ... 11 more
02-13 12:36:47.943: E/AndroidRuntime(14790): Caused by: java.lang.reflect.InvocationTargetException
02-13 12:36:47.943: E/AndroidRuntime(14790): at java.lang.reflect.Constructor.constructNative(Native Method)
02-13 12:36:47.943: E/AndroidRuntime(14790): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
02-13 12:36:47.943: E/AndroidRuntime(14790): ... 22 more
02-13 12:36:47.943: E/AndroidRuntime(14790): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.content.res.Resources.loadDrawable(Resources.java:1709)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.view.View.<init>(View.java:1951)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.view.View.<init>(View.java:1899)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.view.ViewGroup.<init>(ViewGroup.java:286)
02-13 12:36:47.943: E/AndroidRuntime(14790): at android.widget.LinearLayout.<init>(LinearLayout.java:120)
这是我的所有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:weightSum="10"
android:orientation="vertical"
android:background="@drawable/fond">
<ImageView
android:id="@+id/imageView9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:scaleType="fitXY"
android:src="@drawable/bandeau" />
LINE 19 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="3"
android:layout_weight="6"
android:layout_gravity="center"
android:orientation="vertical"
android:background="@drawable/fondshare" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView5"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:src="@drawable/cadre" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:layout_weight="1"
android:src="@drawable/niceshot" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:weightSum="5"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView7"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:src="@drawable/enterthename" />
<EditText
android:id="@+id/editText1"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="4dp" />
<ImageView
android:id="@+id/imageView2"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="4dp"
android:adjustViewBounds="true"
android:src="@drawable/sharefacebook" />
<ImageView
android:id="@+id/imageView3"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="4dp"
android:adjustViewBounds="true"
android:src="@drawable/sharetwitter" />
<ImageView
android:id="@+id/imageView6"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="4dp"
android:adjustViewBounds="true"
android:src="@drawable/sharefacebook" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/imageView10"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:scaleType="fitEnd"
android:src="@drawable/dim" />
</LinearLayout>
我的错误是因为动画可绘制吗?我怎么能“清除”它的记忆或回收它呢?
修改
我的问题来自
BitmapFactory.decodeFile(file.getAbsolutePath());
知道我应该清除什么,因为我已经回收了其他位图
答案 0 :(得分:0)
我认为xmlns应该只在第一个布局中。 尝试从第19行开始替换你的块:
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="3"
android:layout_weight="6"
android:layout_gravity="center"
android:orientation="vertical"
android:background="@drawable/fondshare" >
答案 1 :(得分:0)
听起来你有内存泄漏。问题是没有处理很多图像,而是当你的活动被破坏时你的图像没有被解除分配。
这可能会有所帮助,
http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html
要避免此问题,您可以在使用Bitmap对象(或设置其他值)之前使用本机方法Bitmap.recycle()。例如:
public final void setMyBitmap(Bitmap bitmap) {
if (this.myBitmap != null) {
this.myBitmap.recycle();
}
this.myBitmap = bitmap;
}
调用垃圾收集器是一个好主意。
public void clearAllResources() {
// Set related variables null
System.gc();
Runtime.getRuntime().gc();
}
答案 2 :(得分:0)