在我的 onCreate() 中,我只是使用 xml 作为 Activity 的内容 View 。为什么我会发生内存泄漏。是我做错了什么吗?
我的 xml 只有 5 个图像按钮,每个图像按钮都有一个 statelistdrawable(选择器)。每张图片只有 16kb 大小。 (所以我使用了 10 张图像——每个按钮的按下状态图像和正常状态图像)。
在我的 Activity 中我没有使用 LayoutInflator.. 当从 xml 设置内容 View 时,android 不应该自己回收 bimaps 吗?
我收到类似这样的错误:
02-17 09:18:39.797: ERROR/AndroidRuntime(372): Uncaught handler: thread main exiting due to uncaught exception
02-17 09:18:39.817: ERROR/AndroidRuntime(372): java.lang.RuntimeException: Unable to start activity ComponentInfo{***.***.*****.activity/***.***.*****.home.HomeScreenActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class java.lang.reflect.Constructor
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.os.Looper.loop(Looper.java:123)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.app.ActivityThread.main(ActivityThread.java:4203)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at java.lang.reflect.Method.invoke(Method.java:521)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at dalvik.system.NativeStart.main(Native Method)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class java.lang.reflect.Constructor
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.view.LayoutInflater.createView(LayoutInflater.java:512)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:562)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.app.Activity.setContentView(Activity.java:1620)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at ***.***.*****.home.HomeScreenActivity.onCreate(HomeScreenActivity.java:35)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): ... 11 more
02-17 09:18:39.817: ERROR/AndroidRuntime(372): Caused by: java.lang.reflect.InvocationTargetException
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.widget.RelativeLayout.<init>(RelativeLayout.java:171)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at java.lang.reflect.Constructor.constructNative(Native Method)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.view.LayoutInflater.createView(LayoutInflater.java:499)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): ... 21 more
02-17 09:18:39.817: ERROR/AndroidRuntime(372): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:439)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:322)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:688)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.content.res.Resources.loadDrawable(Resources.java:1710)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.content.res.TypedArray.getDrawable(TypedArray.java:548)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.view.View.<init>(View.java:1846)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.view.View.<init>(View.java:1795)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): at android.view.ViewGroup.<init>(ViewGroup.java:282)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): ... 25 more
我替换了图像,并且代码在应用程序没有挂起的情况下工作,但是我的 ddms 日志仍然因以下错误而变得疯狂:
02-17 09:18:37.287: ERROR/(372): VM won't let us allocate 270000 bytes
02-17 09:18:37.287: ERROR/dalvikvm-heap(372): 270000-byte external allocation too large for this process.
02-17 09:18:37.287: ERROR/(372): VM won't let us allocate 270000 bytes
02-17 09:18:37.287: ERROR/dalvikvm-heap(372): 270000-byte external allocation too large for this process.
02-17 09:18:37.287: ERROR/(372): VM won't let us allocate 270000 bytes
02-17 09:18:37.287: ERROR/dalvikvm-heap(372): 270000-byte external allocation too large for this process.
02-17 09:18:37.287: ERROR/(372): VM won't let us allocate 270000 bytes
02-17 09:18:37.287: ERROR/dalvikvm-heap(372): 264600-byte external allocation too large for this process.
.. . . . .
虽然该应用程序正在运行,但我想知道我做错了什么。还是有问题的 android 系统中的错误?
附上代码:
public class VirtualHomeActivity extends Activity {
ImageButton imgbtn1, imgbtn2, imgbtn3, imgbtn4,
imgbtn5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
}
布局home.xml:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layoutAnimation="@anim/home_screen_animation_controller" android:background="@drawable/bg_application">
<ImageButton android:id="@+id/imgBtn1"
android:layout_width="340dip" android:scaleType="fitCenter" android:layout_height="100dip" android:layout_marginLeft="20dip" android:layout_marginTop="5dip" android:background="@drawable/custombutton1"></ImageButton>
<ImageButton android:layout_below="@id/imgBtn1"
android:id="@+id/imgBtn2" android:layout_width="340dip" android:scaleType="fitCenter" android:layout_height="100dip" android:layout_marginTop="5dip" android:layout_marginLeft="20dip" android:background="@drawable/custombutton2"></ImageButton>
<ImageButton android:layout_below="@id/imgBtn2 android:id="@+id/imgBtn3" android:scaleType="fitCenter" android:layout_width="340dip" android:layout_height="100dip" android:layout_marginLeft="20dip" android:layout_marginTop="5dip" android:background="@drawable/custombutton3"></ImageButton>
<ImageButton android:layout_below="@id/imgBtnS3" android:id="@+id/imgBtn4" android:scaleType="fitCenter" android:layout_height="100dip" android:layout_width="340dip" android:layout_marginLeft="20dip" android:layout_marginTop="5dip" android:background="@drawable/custombutton4"></ImageButton>
<ImageButton android:layout_below="@id/imgBtn4"
android:id="@+id/imgBtn5" android:scaleType="fitCenter"
android:layout_height="100dip" android:layout_width="340dip"
android:layout_marginLeft="20dip" android:layout_marginTop="5dip" android:background="@drawable/custombutton5"></ImageButton>
</RelativeLayout>
选择器是:(custombutton1.xml)
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/img_clicked" />
<item android:drawable="@drawable/img_unclicked" /> </selector>
我从 Activity 中删除了所有其他代码,以检查我是否正在造成内存泄漏。但是在这里我只是将内容 View 设置为我的布局文件,但我的 ddms 仍然说 VM 不会让我们分配 270000 字节
我不认为我试图在这里管理任何 Activity 生命周期。
最佳答案
您的内存泄漏可能在其他地方,也许在您管理 Activity 生命周期更改的方式中。看文章Avoiding Memory Leaks .
关于android - 为什么我在简单地执行 setContentView(R.layout.somelayout) 时会出现内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5269284/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or