简体   繁体   中英

App crashing when setting new content view

Edit:

It seems my button styles are causing the issue.

Edit Code

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;
        }
    });

The code above is what works for 4.2 and up for some reason.

Putting it here for future people.


My app is crashing when I try to call the setContentView() method to my layout .

I tried a few solutions and when I removed my styles for my buttons the error went away.

It seems that the styles I am using for my buttons is causing this.

<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>

My 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

From logcat the problem is background of your Button. I think it's somehow big image so it overflow your memory.

First
Try to remove any @drawable in menu_system.xml especially android:background in Button
Run your if it's work then you know where to look.

Second(for workaround)
Move your background image file to bigger folder
from drawable to drawable-xhdpi

Try this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    Thread splash=new Thread(){
        public void run(){
            try{
                sleep(2000);
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                startActivity(new Intent(getApplicationContext(),StartMenu.class));
                finish();
            }
        }
    };

    splash.start();
}

And your Manifest.xml:

 <activity
 android:name="com.example.mapcard.Splash"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>

我尝试了您的代码。它工作正常。我认为问题是由于内存不足。问题与图像资源有关,请尝试将图像设置为压缩格式以避免内存泄漏。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM