【问题标题】:How to get layout children - getChildAt() returning null如何获取布局子级 - getChildAt() 返回 null
【发布时间】:2014-05-15 16:45:57
【问题描述】:

我正在尝试使用 getChildAt 在 onCreateView 中访问 LinearLayout 的子级

currPaint = (ImageButton)paintLayout.getChildAt(0);

但我不知道为什么 currPaint 为空。

我可以做些什么来联系孩子?

即使我尝试调用 getChildCount,结果也是空的。我究竟做错了什么?

@Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {

                View rootView = inflater.inflate(R.layout.fragment_main, container,
                        false); 

                drawView = (DrawingView)rootView.findViewById(R.id.drawing);
                LinearLayout paintLayout = (LinearLayout)rootView.findViewById(R.id.paint_colors);//get the palette and first color button

                currPaint = (ImageButton)paintLayout.getChildAt(0); 
                currPaint.setImageDrawable(rootView.getResources().getDrawable(R.drawable.paint_pressed));

                return rootView;
            }

这是xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFCCCCCC"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <!-- Top Buttons -->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/new_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:contentDescription="@string/start_new"
            android:src="@drawable/new_pic" />

        <ImageButton
            android:id="@+id/draw_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:contentDescription="@string/brush"
            android:src="@drawable/brush" />

        <ImageButton
            android:id="@+id/erase_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:contentDescription="@string/erase"
            android:src="@drawable/eraser" />

        <ImageButton
            android:id="@+id/save_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:contentDescription="@string/save"
            android:src="@drawable/save" />
    </LinearLayout>

    <!-- Custom View -->

    <com.example.tastycorpse.DrawingView
        android:id="@+id/drawing"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="3dp"
        android:layout_weight="1"
        android:background="#FFFFFFFF" />

    <!-- Color Palette -->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical" >

        <!-- Top Row -->

        <LinearLayout
            android:id="@+id/paint_colors"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF660000"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF660000" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FFFF0000"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FFFF0000" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FFFF6600"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FFFF6600" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FFFFCC00"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FFFFCC00" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF009900"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF009900" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF009999"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF009999" />
        </LinearLayout>

        <!-- Bottom Row -->

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF0000FF"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF0000FF" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF990099"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF990099" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FFFF6666"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FFFF6666" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FFFFFFFF"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FFFFFFFF" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF787878"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF787878" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF000000"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF000000" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: android android-linearlayout imagebutton children


    【解决方案1】:

    为什么不给按钮一个 ID,然后通过以下方式获得对它的引用:

    currPaint = (ImageButton)paintLayout.findViewById(R.id.brush_button);
    

    【讨论】:

    • 这是一个有效的解决方案并且更稳定(我想我会使用它),但我无法理解这段代码不起作用的原因。这应该是正确的
    • 我似乎记得刚开始使用 Android 时曾发现一棵类似的树,但我也从未真正深入了解它
    • 我放弃了,我使用了 findViewById,但我不明白为什么它不起作用
    猜你喜欢
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    相关资源
    最近更新 更多