0

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

CustomView -- artoo 有条件的:

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();
    }

支持xml布局:

<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>

日志猫:

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 第一次膨胀时,它的父级变为空,因此在单击“否”后单击“是”时会导致错误。创建这样的视图的最佳方法是什么?

任何帮助将不胜感激。谢谢

萨米尔

4

1 回答 1

1

利用

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

因为该inflate()方法具有返回膨胀布局的根视图的好处

于 2011-06-14T10:06:07.217 回答