自定义视图-条件视图--ViewStub &单选按钮

发布于 2024-09-16 08:52:00 字数 7073 浏览 6 评论 0原文

我正在创建一个自定义视图,该视图根据条件“是”或“否”进行填充,通过 RadioButtons 和 RadioButtons 实现。视图存根。不幸的是,事情没有按计划进行:

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)

我可以看到,第一次 ViewStub 膨胀时,它的父级变为 null,因此当我单击“是”后单击“是”时会导致错误不”。创建这样的视图的最佳方法是什么?

任何帮助将不胜感激。谢谢萨梅尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡笑忘祈一世凡恋 2024-09-23 08:52:00

用作

View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();

inflate() 方法的好处是返回 inflate 布局的根视图

use

View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();

as the inflate() method has the benefit of returning the root View of the inflate layout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文