Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: StackOverFlowError with InvocationTargetException when inflating layout

First, sorry for my English. I have a question when inflating layout:

I have a custom view, extends from a LinearLayout, called ButtonHelp. I use that view on my layout named LoadingActivity. My below codes work perfectly on every devices and emulators, except ones with QVGA screen like Samsung Galaxy Y with following errors:

FATAL EXCEPTION: main  
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.LoadingActivity}: android.view.InflateException: Binary XML file line #45: Error inflating class com.myapp.customviews.ButtonHelp  
    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:123)
    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 #45: Error inflating class com.myapp.customviews.ButtonHelp
    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    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.myapp.LoadingActivity.onCreate(LoadingActivity.java:45)
    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)
    ... 21 more
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.widget.ImageButton
    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.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.myapp.customviews.ButtonHelp.initLayout(ButtonHelp.java:29)
    at com.myapp.customviews.ButtonHelp.<init>(ButtonHelp.java:17)
    ... 24 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)
    ... 32 more
Caused by: java.lang.StackOverflowError
    at android.util.LongSparseArray.get(LongSparseArray.java:79)
    at android.content.res.Resources.getCachedDrawable(Resources.java:1746)
    at android.content.res.Resources.loadDrawable(Resources.java:1664)
    at android.content.res.Resources.getDrawable(Resources.java:581)
    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
    at android.content.res.Resources.loadDrawable(Resources.java:1694)
    at android.content.res.Resources.getDrawable(Resources.java:581)
    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.

The problem is, I have no idea about this line:

Caused by: java.lang.StackOverflowError
    at android.util.LongSparseArray.get(LongSparseArray.java:79)

Why does it happen on QVGA emulators/devices?

FYI:

  • I have image resources for screen 320x480 only.
  • ButtonHelp layout:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    
    <ImageButton
        android:id="@+id/btnhelp_help"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/loading_button_help"
        android:src="@drawable/loading_icon_help" />
    </LinearLayout>
    
  • ButtonHelp code:

    public class ButtonHelp extends LinearLayout {
        private ImageButton mHelp;
    
        public ButtonHelp(Context context, AttributeSet attrs) {
            super(context, attrs);
            initLayout();
            initControls();
        }
    
        public ButtonHelp(Context context) {
            super(context);
            initLayout();
            initControls();
        }
    
        private void initLayout() {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.btn_help, this);
        }
    
        private void initControls() {
            mHelp = (ImageButton) findViewById(R.id.btnhelp_help);
        }
    
        public void setOnClickListener(View.OnClickListener listener) {
            mHelp.setOnClickListener(listener);
        }
    }
    
  • ButtonHelp is used in LoadingActivity:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/loading_background"
        android:orientation="vertical" >
    
    ...
    
        <com.myapp.customviews.ButtonHelp
            android:id="@+id/loading_help"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true" />
    
    ...
    
    </RelativeLayout>
    
  • When I remove ButtonHelp, the error still appears when creating layout, just like above logcat.

I've searched another SO questions but still have no solution or explanation for this case.

Many thanks!


UPDATE:

As asked, below are codes for loading_button_help:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/loading_button_help" /> <!-- Normal -->

    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/loading_button_help" /> <!--  Normal disable -->

    <item android:state_pressed="true" 
        android:drawable="@drawable/loading_button_help_press" /> <!-- pressed -->

    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/loading_button_help" /> <!-- selected -->

    <item android:state_enabled="true"
        android:drawable="@drawable/loading_button_help" /> <!-- normal -->

    <item android:state_focused="true"
        android:drawable="@drawable/loading_button_help" /> <!-- normal disable focused -->

    <item
        android:drawable="@drawable/loading_button_help" /> <!-- normal disable -->
</selector>

And loading_icon_help is just a PNG image.

like image 418
thaiduy Avatar asked Oct 09 '22 13:10

thaiduy


1 Answers

In loading_button_help.xml you use @drawable/loading_button_help? I think you have cyclic reference. Try to rename your xml or image file.

like image 185
Vyacheslav Shylkin Avatar answered Oct 12 '22 10:10

Vyacheslav Shylkin