这是我的问题,我的应用程序启动完美,活动在调用它们时工作正常,但当我回到主要活动并启动另一个时,我的应用程序崩溃。
这是logcat中的错误:
06-29 23:48:46.885: E/AndroidRuntime(860): FATAL EXCEPTION: main
06-29 23:48:46.885: E/AndroidRuntime(860): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whaoapps.maths/com.whaoapps.maths.Integrales}: android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
06-29 23:48:46.885: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.os.Looper.loop(Looper.java:137)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-29 23:48:46.885: E/AndroidRuntime(860): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 23:48:46.885: E/AndroidRuntime(860): at java.lang.reflect.Method.invoke(Method.java:511)
06-29 23:48:46.885: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-29 23:48:46.885: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-29 23:48:46.885: E/AndroidRuntime(860): at dalvik.system.NativeStart.main(Native Method)
06-29 23:48:46.885: E/AndroidRuntime(860): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
06-29 23:48:46.885: E/AndroidRuntime(860): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
06-29 23:48:46.885: E/AndroidRuntime(860): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-29 23:48:46.885: E/AndroidRuntime(860): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.app.Activity.setContentView(Activity.java:1881)
06-29 23:48:46.885: E/AndroidRuntime(860): at com.whaoapps.maths.Integrales.onCreate(Integrales.java:14)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.app.Activity.performCreate(Activity.java:5104)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-29 23:48:46.885: E/AndroidRuntime(860): ... 11 more
06-29 23:48:46.885: E/AndroidRuntime(860): Caused by: java.lang.reflect.InvocationTargetException
06-29 23:48:46.885: E/AndroidRuntime(860): at java.lang.reflect.Constructor.constructNative(Native Method)
06-29 23:48:46.885: E/AndroidRuntime(860): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
06-29 23:48:46.885: E/AndroidRuntime(860): ... 25 more
06-29 23:48:46.885: E/AndroidRuntime(860): Caused by: java.lang.OutOfMemoryError
06-29 23:48:46.885: E/AndroidRuntime(860): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.content.res.Resources.loadDrawable(Resources.java:1965)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.view.View.<init>(View.java:3330)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.view.View.<init>(View.java:3259)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.view.ViewGroup.<init>(ViewGroup.java:425)
06-29 23:48:46.885: E/AndroidRuntime(860): at android.widget.RelativeLayout.<init>(RelativeLayout.java:210)
06-29 23:48:46.885: E/AndroidRuntime(860): ... 28 more
这是xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/maths"
android:background="@android:color/white" />
<Button
android:id="@+id/button2"
android:layout_width="205dp"
android:layout_height="50dp"
android:layout_alignLeft="@+id/button1"
android:layout_centerVertical="true"
android:background="@android:color/transparent"
android:onClick="derivadas" />
<Button
android:id="@+id/button1"
android:layout_width="205dp"
android:layout_height="50dp"
android:layout_above="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="58dp"
android:background="@android:color/transparent"
android:onClick="integrales" />
<com.google.ads.AdView android:id="@+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="my_unit_id"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"/>
</RelativeLayout>
android清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.whaoapps.maths"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/maths_icono"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|s mallestScreenSize" />
<activity
android:name="com.whaoapps.maths.MainActivity"
android:screenOrientation="portrait"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.whaoapps.maths.Derivadas"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Light.NoTitleBar"></activity>
<activity
android:name="com.whaoapps.maths.Integrales"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Light.NoTitleBar"></activity>
</application>
</manifest>
答案 0 :(得分:0)
您的应用程序在扩充布局时超出了内存预算,如堆栈跟踪中所示:
06-29 23:48:46.885: E/AndroidRuntime(860): Caused by: java.lang.OutOfMemoryError
特别是在将位图加载到内存时发生了这种情况。以下行表明内存限制在布局的第16行上命中。
06-29 23:48:46.885: E/AndroidRuntime(860): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
最有可能(我没有计算),这将指向设置为ImageView
的图像:
android:src="@drawable/maths"
maths
可绘制的图像是否相当大(如像素尺寸)?您可能希望将其缩小并为各种密度桶提供不同的版本。另请注意,如果您在模拟器上进行测试,则可能需要稍微提高堆大小设置。这可能会阻止应用程序崩溃,但显然它无法解决使用(太多)内存的应用程序的潜在问题。最后,请查看Android版开发者网站上的Loading Large Bitmaps Efficiently guide