0

我在我的活动“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());

知道我应该清除什么,因为我已经回收了其他位图

4

3 回答 3

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();
}
于 2013-02-13T13:03:16.810 回答
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" >  
于 2013-02-13T12:49:21.890 回答
0

我认为问题出在最新的更新 ADT

http://android.foxykeep.com/dev/how-to-fix-the-classdefnotfounderror-with-adt-17

于 2013-05-27T19:47:39.440 回答