在我的应用程序中,当我尝试启动它时强制关闭并且错误指向“setContentView(R.layout.Menu);”行的布局。在 XML 文件中,它在我的布局中显示“OutOfMemoryError” ImageView 。我真的很困惑。请指导我进一步行动。
已编辑:
我的应用程序使用数据库,并在第一次解析一些 XML 数据并插入到 Sqlite 数据库中。我的内存不足问题仅在第一次出现。第二次它工作正常。我试过 System.gc()。有什么问题吗?
这是我的日志:
E/dalvikvm-heap(2712): 105376-byte external allocation too large for this process.
VM won't let us allocate 105376 bytes
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Test/com.Test.Menu}: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
at android.app.Activity.setContentView(Activity.java:1657)
at com.Test.Menu.onCreate(Menu.java:32)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
... 23 more
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
at android.content.res.Resources.loadDrawable(Resources.java:1709)
at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
at android.widget.ImageView.<init>(ImageView.java:118)
at android.widget.ImageView.<init>(ImageView.java:108)
这是我的 XML 代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/RL_Title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="8"
android:onClick="onTitleClick" >
<ImageView
android:id="@+id/Img_Title_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
android:src="@drawable/title_bg" />
<Button
android:id="@+id/Btn_Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:background="@drawable/title_al"
android:drawableRight="@drawable/pro"
android:gravity="center"
android:onClick="onTitleClick" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/RL_MainMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" android:onClick="onDoNothing">
<ImageView
android:id="@+id/ImageView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:src="@drawable/main_bg" android:scaleType="centerCrop"/>
<ImageView
android:id="@+id/Img_logo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:scaleType="center"
android:src="@drawable/logo_al" />
<LinearLayout
android:id="@+id/LI_Menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/RL_ExtraOption"
android:layout_alignTop="@+id/Img_logo"
android:layout_margin="2dp"
android:orientation="vertical" >
<ImageButton
android:id="@+id/Img_Buyer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="@drawable/bt_blink"
android:onClick="Nextclick"
android:scaleType="fitCenter"
android:soundEffectsEnabled="true"
android:src="@drawable/buyer_icon" />
<ImageButton
android:id="@+id/Img_Seller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="@drawable/bt_blink"
android:onClick="Nextclick"
android:scaleType="fitCenter"
android:src="@drawable/seller_icon" />
<ImageButton
android:id="@+id/Img_Lender"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="@drawable/bt_blink"
android:onClick="Nextclick"
android:scaleType="fitCenter"
android:src="@drawable/lender_icon" />
<ImageButton
android:id="@+id/Img_myTitleRep"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="@drawable/bt_blink"
android:onClick="Nextclick"
android:scaleType="fitCenter"
android:src="@drawable/my_title_rep_icon_al" />
<ImageButton
android:id="@+id/Img_Setup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="@drawable/bt_blink"
android:onClick="Nextclick"
android:scaleType="fitCenter"
android:src="@drawable/setup_icon" />
</LinearLayout>
<RelativeLayout
android:id="@+id/RL_ExtraOption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/main_bottom_bg" >
<TextView
android:id="@+id/txt_RepName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textColor="@color/white"
android:textSize="@dimen/font_size" />
<TableRow
android:id="@+id/TR_ContactRep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center" >
<Button
android:id="@+id/Btn_ContactRep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:background="@drawable/contact_rep_blink"
android:onClick="ContactRep_Click" />
<Button
android:id="@+id/Btn_MoreOption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="@drawable/main_more_blink"
android:onClick="onMoreClick" />
</TableRow>
</RelativeLayout>
<LinearLayout
android:id="@+id/ln_Mainmore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog_bg" android:layout_alignParentBottom="true" android:visibility="gone">
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical" >
</LinearLayout>
<TableLayout
android:id="@+id/TableLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<TableRow
android:id="@+id/TableRow04"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_marginTop="20dp"
android:gravity="center" >
<Button
android:id="@+id/Btn_Rate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/property_blue_blink"
android:onClick="onRate"
android:singleLine="true"
android:text="Rate/Testimonial"
android:textColor="@color/white"
android:textSize="@dimen/font_size"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="@+id/TableRow01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:gravity="center" >
<Button
android:id="@+id/btn_SubFeature"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/property_blue_blink"
android:onClick="onSubFeature"
android:singleLine="true"
android:text="Submit A Feature"
android:textColor="@color/white"
android:textSize="@dimen/font_size"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="@+id/TableRow03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:gravity="center" >
<Button
android:id="@+id/Btn_ReferFrnd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/property_blue_blink"
android:onClick="onReferAFrnd"
android:text="Refer A Friend"
android:textColor="@color/white"
android:textSize="@dimen/font_size"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="@+id/TableRow02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:gravity="center" >
<Button
android:id="@+id/Btn_cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/property_cancel_blink"
android:onClick="onClose"
android:text="Cancel"
android:textColor="@color/black"
android:textSize="@dimen/font_size"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
最佳答案
我猜问题不在于你的布局;问题出在代码中的其他地方。而且您可能在某处泄露了上下文。
其他可能的原因是您在解析 XML 时必须创建庞大的多个对象(正如您提到的,当您第一次解析 XML 时会发生这种情况)。虽然 Java 有自动垃圾回收的方法,但你仍然不能完全依赖它。当您不再需要它们时,使您的集合实例无效或清除您的对象内容是一种很好的做法。
但我仍然准备了一个重要的点列表,在 Android 上处理位图时应该记住这些点。
1) 您可以在每个位图上调用回收并将它们设置为空。 (bitmap.recycle() 会释放此位图使用的所有内存,但不会使位图对象无效)。
2) 您还可以在销毁 Activity 时取消绑定(bind)与布局关联的drawables。试试下面给出的代码,也看看这个链接link .
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
// Call this method from onDestroy()
void onDestroy() {
super.onDestroy();
unbindDrawables(findViewById(R.id.RootView));
System.gc();
}
3)您可以将您的hashmaps转换为WeakHashmaps,这样当系统内存不足时,它的内存就会被释放。
4) 您可以缩放/调整所有位图的大小。要缩放位图,您可以尝试以下操作:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap=BitmapFactory.decodeStream(is, null, options);
此 inSampleSize 选项可减少内存消耗。
这是一个完整的方法。首先它读取图像大小而不解码内容本身。然后它找到最好的 inSampleSize 值;它应该是 2 的幂。最后图像被解码。
// Decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
// The new size we want to scale to
final int REQUIRED_SIZE=70;
// Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >= REQUIRED_SIZE)
scale*=2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
}
catch (FileNotFoundException e) {
}
return null;
}
看看这个link. .
5) 您可以在整个系统内存不足时调用的 Activity 中覆盖 onLowMemory() 方法。你可以在那里释放一些资源。
6) 您可以将对象设置为 SoftReference 或 Weakreference,以便在内存不足的情况下释放它们。
我观察到的一个非常常见的内存泄漏是由于内部类的实现和 Activity 中的 Handler 实现。 This links talks about the same in more detail .
我希望这有助于解决您的问题。
关于android - 由 : java. lang.OutOfMemoryError 引起:位图大小超出 VM 预算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10314527/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候
遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg
Region是HBase数据管理的基本单位,region有一点像关系型数据的分区。region中存储这用户的真实数据,而为了管理这些数据,HBase使用了RegionSever来管理region。Region的结构hbaseregion的大小设置默认情况下,每个Table起初只有一个Region,随着数据的不断写入,Region会自动进行拆分。刚拆分时,两个子Region都位于当前的RegionServer,但处于负载均衡的考虑,HMaster有可能会将某个Region转移给其他的RegionServer。RegionSplit时机:当1个region中的某个Store下所有StoreFile
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路