views:

19

answers:

0

Hi,

I am creating a custom view which populates based on a condition "Yes" or "No", implemented through RadioButtons & ViewStub. Unfortunately things are not going as planned:

CustomView -- artooConditional:

public artooConditional(Context context, AttributeSet attrs) {
        super(context, attrs);
        setAttributes(attrs);
    }

    private void setAttributes(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs,
                R.styleable.artooConditional);
        mLayout = a.getResourceId(R.styleable.artooConditional_layout, NO_ID);

        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(getContext().LAYOUT_INFLATER_SERVICE);

        View v = inflater.inflate(R.layout.types_conditional, null, false);
        addView(v, new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        RadioGroup group = (RadioGroup) v
                .findViewById(R.id.types_conditional_group);
        layout = (ViewStub) v.findViewById(R.id.types_conditional_layout);

        group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.types_conditional_yes) {
                    if (!inflated) {
                        layout.setLayoutResource(mLayout);
                        layout.inflate();
                        inflated = true;
                    }
                    // layout.setVisibility(View.VISIBLE);
                } else {
                    // if (layout.getVisibility() != View.GONE)
                    layout.setVisibility(View.GONE);
                    inflated = false;
                }

            }
        });
        a.recycle();
    }

Supporting xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:a="http://schemas.android.com/apk/res/com.android.artoo.types.buttondialog"
    android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
    <RadioGroup android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/types_conditional_group">
        <RadioButton android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Yes" android:layout_weight="1" android:id="@+id/types_conditional_yes"/>
        <RadioButton android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="No" android:layout_weight="1" android:id="@+id/types_conditional_no"/>
    </RadioGroup>
    <ViewStub android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/types_conditional_layout" android:visibility="gone"/>
</LinearLayout>

LogCat:

08-25 16:14:39.579: ERROR/AndroidRuntime(356): Uncaught handler: thread main exiting due to uncaught exception
08-25 16:14:39.599: ERROR/AndroidRuntime(356): java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.view.ViewStub.inflate(ViewStub.java:246)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at com.android.artoo.types.buttondialog.artooConditional$1.onCheckedChanged(artooConditional.java:67)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.widget.RadioGroup.access$600(RadioGroup.java:52)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.widget.CompoundButton.setChecked(CompoundButton.java:125)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.widget.CompoundButton.toggle(CompoundButton.java:85)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.widget.RadioButton.toggle(RadioButton.java:69)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.widget.CompoundButton.performClick(CompoundButton.java:97)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.view.View.onTouchEvent(View.java:4133)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.widget.TextView.onTouchEvent(TextView.java:6510)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.view.View.dispatchTouchEvent(View.java:3672)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.os.Looper.loop(Looper.java:123)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at android.app.ActivityThread.main(ActivityThread.java:4203)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at java.lang.reflect.Method.invokeNative(Native Method)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at java.lang.reflect.Method.invoke(Method.java:521)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
08-25 16:14:39.599: ERROR/AndroidRuntime(356):     at dalvik.system.NativeStart.main(Native Method)

I can see, the first time the ViewStub is inflated, its parent becomes null and thus is causing an error when I click "Yes" after clicking "No". Whats the best way to create such a view?

Any help will be most appreciated. Thanks

Sameer