4

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

bluebyte
  • 560
  • 2
  • 7
  • 23

2 Answers2

5

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.

user
  • 86,916
  • 18
  • 197
  • 190
0

The presence of this line

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

in your constructor for the custom layout object is causing a infinite recursion of self-dependent calls, overflowing the stack.

There's no reason for you to be doing that.

Probably you best bet is to dig up an example of someone else's working custom layout class.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • without this line - i got null when trying to findViewById – bluebyte May 14 '12 at 17:48
  • Perhaps, but that is not the right way to resolve it. I'm suggesting you find someone's working example of a custom layout as a guide to the mechanics of how it is supposed to be done. – Chris Stratton May 14 '12 at 17:57