3

I am trying to create a compound viewgroup after inflating the group from an XML file.

The viewgroup is composed as: A LinearLayout Root, 2 child LinearLayouts. I am able to see the layout correctly in the layout editor; however, when I attempt to add a view (say a button) from the editor, the view does not show up and the application immediately force closes. I was told i may need to Override the onLayout method to correctly draw the view components but I'm am fairly confused.

My Class:

public class FocusBox extends LinearLayout {
private LinearLayout    rootLayout,
                        contentLayout,
                        topLayout;

public FocusBox(Context context)
{
    super(context, null);
}
public FocusBox(Context context, AttributeSet attrs) {
    super(context, attrs);

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.focus_box, this);

rootLayout = (LinearLayout)findViewById(R.id.focusBoxParent);
contentLayout = (LinearLayout)findViewById(R.id.focusBottom);
topLayout = (LinearLayout)findViewById(R.id.focusTop);

}
}

And the xml:

<?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="match_parent"
    android:padding="4dp"
    android:id="@+id/focusBoxParent"
    android:orientation="vertical">
    <LinearLayout
        android:background="@drawable/gradients"
        android:layout_weight=".1"
        android:gravity="center"
        android:id="@+id/focusTop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:text="TextView"
            android:id="@+id/focusTitle"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_width="wrap_content"/>
    </LinearLayout>
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_weight=".9"
        android:id="@+id/focusBottom"
        android:background="@drawable/gradient2"
        android:orientation="horizontal">
    </LinearLayout>
</LinearLayout>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Dizzle797
  • 31
  • 2
  • did you solve the problem yet? i just tried the very same code you posted above and it works flawlessly. i didn't copy your code but came to this solution independently, and the resulting code is almost perfectly the same. i checked for any noteworthy differences, but there are none. – stefs Feb 22 '12 at 17:14

1 Answers1

0

Generally, if you inflate a layout from a valid XML you shouldn't get an error. You should do a clean build and re-deploy the app again. Also check if you're using the correct class in the import statement in other classes (you could be using a FocusBox from some 3rd-party library instead of the one you made)

milosmns
  • 3,595
  • 4
  • 36
  • 48