编辑:
看来我的按钮样式引起了问题。
编辑代码
buttonL.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_DOWN) buttonL.setBackgroundResource(R.drawable.lights_over); if(event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) buttonL.setBackgroundResource(R.drawable.lights); return false; } });
出于某些原因,以上代码适用于4.2及更高版本。
当我尝试将setContentView()方法调用到 布局 时,我的应用程序崩溃了。
setContentView()
我尝试了一些解决方案,当我删除按钮样式时,错误消失了。
看来我在按钮上使用的样式是造成这种情况的原因。
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/lights_over" /> <item android:state_focused="true" android:drawable="@drawable/lights_over" /> <item android:state_selected = "true" android:drawable = "@drawable/lights_over" /> <item android:drawable = "@drawable/lights" android:id="@+id/button_L"/> </selector>
我的LogCat
01-09 12:04:24.161: D/dalvikvm(20682): GC_BEFORE_OOM freed 9K, 5% free 122880K/129056K, paused 20ms, total 20ms 01-09 12:04:24.161: E/dalvikvm-heap(20682): Out of memory on a 9027940-byte allocation. 01-09 12:04:24.171: E/AndroidRuntime(20682): FATAL EXCEPTION: main 01-09 12:04:24.171: E/AndroidRuntime(20682): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.broadcastreceiver/com.androidexample.broadcastreceiver.StartMenu}: android.view.InflateException: Binary XML file line #79: Error inflating class <unknown> 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.app.ActivityThread.access$700(ActivityThread.java:151) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.os.Handler.dispatchMessage(Handler.java:99) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.os.Looper.loop(Looper.java:137) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.app.ActivityThread.main(ActivityThread.java:5293) 01-09 12:04:24.171: E/AndroidRuntime(20682): at java.lang.reflect.Method.invokeNative(Native Method) 01-09 12:04:24.171: E/AndroidRuntime(20682): at java.lang.reflect.Method.invoke(Method.java:511) 01-09 12:04:24.171: E/AndroidRuntime(20682): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 01-09 12:04:24.171: E/AndroidRuntime(20682): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 01-09 12:04:24.171: E/AndroidRuntime(20682): at dalvik.system.NativeStart.main(Native Method) 01-09 12:04:24.171: E/AndroidRuntime(20682): Caused by: android.view.InflateException: Binary XML file line #79: Error inflating class <unknown> 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.view.LayoutInflater.createView(LayoutInflater.java:619) 01-09 12:04:24.171: E/AndroidRuntime(20682): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.view.LayoutInflater.inflate(LayoutInflater.java:495) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 01-09 12:04:24.171: E/AndroidRuntime(20682): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.app.Activity.setContentView(Activity.java:1928) 01-09 12:04:24.171: E/AndroidRuntime(20682): at com.androidexample.broadcastreceiver.StartMenu.onCreate(StartMenu.java:20) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.app.Activity.performCreate(Activity.java:5250) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210) 01-09 12:04:24.171: E/AndroidRuntime(20682): ... 11 more 01-09 12:04:24.171: E/AndroidRuntime(20682): Caused by: java.lang.reflect.InvocationTargetException 01-09 12:04:24.171: E/AndroidRuntime(20682): at java.lang.reflect.Constructor.constructNative(Native Method) 01-09 12:04:24.171: E/AndroidRuntime(20682): at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.view.LayoutInflater.createView(LayoutInflater.java:593) 01-09 12:04:24.171: E/AndroidRuntime(20682): ... 25 more 01-09 12:04:24.171: E/AndroidRuntime(20682): Caused by: java.lang.OutOfMemoryError 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.content.res.Resources.loadDrawable(Resources.java:2854) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.content.res.Resources.getDrawable(Resources.java:1521) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:173) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:921) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:858) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.content.res.Resources.loadDrawable(Resources.java:2839) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.content.res.TypedArray.getDrawable(TypedArray.java:602) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.view.View.<init>(View.java:3460) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.widget.TextView.<init>(TextView.java:791) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.widget.Button.<init>(Button.java:107) 01-09 12:04:24.171: E/AndroidRuntime(20682): at android.widget.Button.<init>(Button.java:103) 01-09 12:04:24.171: E/AndroidRuntime(20682): ... 28 more
从logcat出现的问题是Button的背景。我认为这是一个很大的形象,所以它溢出了您的记忆。
首先 尝试删除menu_system.xml中的任何@drawable,尤其是Button中的android:background。 如果可以运行,请运行,然后您知道要查找的位置。
第二(用于解决方法) 将背景图像文件 从drawable移至drawable-xhdpi的较大文件夹