开发者

Crashes whenever I try to use a custom view object in XML layout

I have made myself a custom LinearLayout by the name of com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget that hosts a couple of spinner widgets. This custom LinearLayout inflates the following XML layout (You might not need to look at this too carefully but it's here for completeness):

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

<!-- Min value Spinner -->
<Spinner
    android:id="@+id/discrete_numerical_range_selector_min_value_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:layout_weight="1" />

<TextView
    android:id="@+id/to_text"
    android:text="to"
    android:textSize="14sp"
    android:paddingLeft="10sp"
    android:paddingRight="10sp"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="0">
</TextView>

<!-- Max value Spinner -->
<Spinner
    android:id="@+id/discrete_numerical_range_selector_max_value_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:layout_weight="1" />

I have placed one such object in the layout for one of my activities like so:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="ver开发者_如何学Gotical">

<include layout="@layout/search_form_section_generic_top"/>

<include layout="@layout/search_form_section_car_specific"/>

<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget/>

<include layout="@layout/search_form_section_advanced_options" />

</LinearLayout>

The problem is that my app force closes immediately upon startup. I've checked by putting breakpoints in my custom LinearLayout that none of my custom code is even being run yet. Furthermore, if I copy-paste the layout code for my compound widget in place everything works, which indicates to me that I probably haven't left any important XML attributes out. What could be going wrong?


I fixed it by making the LinearLayout XML element in the widget layout into a merge instead, and moved all of the layout parameters out of the widget XML file and into the activity XML itself, thus replacing

<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget/>

with

<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

If someone could tell me why this worked, it might help me and others doing it again, and you can take credit.


because you must specify the width and height of every view you use in you xml?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜