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 his 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 the activity that 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 delete the comment block from: LayoutInflater.from(context).inflate(R.layout.custom_layout, this, true);and go to my_activity.xml in graphical mode. Eclipse thinks and then falls. It looks like he is trying to inflate my user view many times, but I don’t understand why. I get this error in the error log when I re-run eclipse:java.lang.StackOverflowError

+3
source share
2 answers

In custom_layout.xmlreplace <com.example.MyCustomLayoutwith another layout (e.g. 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>

merge ( orientation MyCustomLayout). , Android my_activity.xml, View . , Android XML custom_layout MyCustomLayout. , <com.example.MyCustomLayout ... ( custom_layout.xml), MyCustomLayout . , , , StackOverflowError.

+4

LayoutInflater.from(context).inflate(R.layout.custom_layout, this, true);

, .

.

, , - .

0

All Articles