Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting stackoverflow error while trying to inflate custom view

I have custom_layout.xml:

<?xml version="1.0" encoding="utf-8"?>

<com.example.MyCustomLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<!-- different views -->

</com.example.MyCustomLayout>

And its class:

public class MyCustomLayout extends LinearLayout {

public MyCustomLayout(Context context, AttributeSet attrs) {
    super(context, attrs);

    LayoutInflater.from(context).inflate(R.layout.custom_layout, this, true);
    setUpViews();
    }
//different methods
}

And activity, which includes this layout:

public class MyActivity extends Activity {

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

    setUpViews();
}

And my_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <com.example.MyCustomLayout
        android:id="@+id/section1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
    <com.example.MyCustomLayout
        android:id="@+id/section2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

</LinearLayout>

So, I have a problem when I remove comment block from: LayoutInflater.from(context).inflate(R.layout.custom_layout, this, true); and go to my_activity.xml in graphic mode. Eclipse thinks and then crashes. It look like it tries to inflate my custom view many times but I don't understand why. I'am getting this error in error log when i relaunch eclipse: java.lang.StackOverflowError

like image 226
bluebyte Avatar asked Oct 21 '25 17:10

bluebyte


1 Answers

In your custom_layout.xml replace <com.example.MyCustomLayout with another layout(for example a LinearLayout):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<!-- different views -->

</LinearLayout>

or even better use the merge tag(and set the orientation in the MyCustomLayout class). Right now when Android loads my_activity.xml it will find your custom View and it will instantiate it. When your custom view will be instantiated Android will inflate the custom_layout xml file in the MyCustomLayout constructor. When this happens it will once again find <com.example.MyCustomLayout ...(from the just inflated custom_layout.xml) which results in MyCustomLayout being instantiated again. This is a recursive call and it will finally throw the StackOverflowError.

like image 108
user Avatar answered Oct 23 '25 08:10

user



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!