当我尝试运行我的应用时出现错误。我在这个主题上看到了其他帖子,但它们都不适用于此代码。一个不适用的解决方案是将android:background属性更改为drawable。这显然不是真的。这是活动代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal"
android:background="@drawable/backg"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="tirg.com.kindust.bobda.tirginvasion.MainMenu">
<Button
android:background="@drawable/lrplaybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_marginBottom="50dp"
style="?android:attr/borderlessButtonStyle"
android:gravity="center"
android:padding="30dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_above="@+id/button2" />
<Button
android:background="@drawable/lrhighscores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:padding="30dp" />
<Button
android:background="@drawable/lrhelpbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_marginTop="50dp"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:padding="30dp" />
</RelativeLayout>
完整错误日志:
01-10 11:58:19.359 3091-3091/tirg.com.kindust.bobda.tirginvasion E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{tirg.com.kindust.bobda.tirginvasion/tirg.com.kindust.bobda.tirginvasion.MainMenu}: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2080)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
at android.app.ActivityThread.access$600(ActivityThread.java:133)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4795)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at tirg.com.kindust.bobda.tirginvasion.MainMenu.onCreate(MainMenu.java:14)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
at android.app.ActivityThread.access$600(ActivityThread.java:133)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4795)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at tirg.com.kindust.bobda.tirginvasion.MainMenu.onCreate(MainMenu.java:14)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
at android.app.ActivityThread.access$600(ActivityThread.java:133)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4795)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
at android.content.res.Resources.loadDrawable(Resources.java:1930)
at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
at android.view.View.<init>(View.java:3336)
at android.view.View.<init>(View.java:3273)
at android.view.ViewGroup.<init>(ViewGroup.java:421)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:184)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at tirg.com.kindust.bobda.tirginvasion.MainMenu.onCreate(MainMenu.java:14)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
at android.app.ActivityThread.access$600(ActivityThread.java:133)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4795)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
谢谢!
答案 0 :(得分:1)
我的照片是5000x7000(太大了!)。通过调整大小来修复!
答案 1 :(得分:0)
查看背景可绘制的大小android:background="@drawable/backg"
。尝试使图像尺寸(分辨率)更小(和/或更小尺寸的背景图像)。另请参阅this。
正如您所看到的,这导致了它:
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
......
您还可以看到this。您还可以尝试从代码设置背景,使用inSampleSize
从here获取帮助,将比例位图加载到内存中。