2

編集:愚かなユーザーエラー、私は不正な形式のXMLを持っていました。

-

3つのフラグメントが含まれているアクティビティがあります。あるフラグメントのonResume()で、アクティビティを使用して別のフラグメントからビューを取得しようとしています。findViewById()ですが、そのメソッドは、特定の1つのフラグメントからのビューに対してnullを返します。他の2つのフラグメントのいずれかのビューは問題ありません。

getRootView()と次のすばやくハッキングされたコードを使用してビューツリー全体をダンプすると、問題のビューは実際にビューツリーの一部であることがわかりますが、IDは-1です。

private void dumpAllViews(View v, int indent)
{
    // Java doesn't support variable width format code -- "%*c"
    // Fake it with string concatenation.  Lame!
    Log.d(tag, String.format("%" + (indent * 4 + 1) + "c%s %d", ' ', v.toString(), v.getId()));
    if (v instanceof ViewGroup)
    {
        ViewGroup vg = (ViewGroup)v;
        for (int i = 0; i < vg.getChildCount(); i++)
        {
            dumpAllViews(vg.getChildAt(i), indent + 1);
        }
    }
}

そのフラグメント内のすべてのビューのIDが-1であるのはなぜですか?

編集:好奇心旺盛な方のために、ここにダンプされたビューツリーがあります。問題のあるビューは、2番目のブロック内のビュー(ID 2131165209)です。

com.android.internal.policy.impl.PhoneWindow$DecorView@4057e118 -1
    android.widget.FrameLayout@4057e4f0 16908290
        android.widget.RelativeLayout@4057eee8 -1
            android.widget.FrameLayout@4057f2e0 2131165208
                android.support.v4.app.NoSaveStateFrameLayout@40594dd0 -1
                    android.widget.RelativeLayout@405933e0 -1
                        android.widget.ImageView@405937d0 -1
                        android.widget.Button@40593b30 2131165223
                        android.widget.Button@40594260 2131165224
                        android.widget.TextView@40594820 -1
            android.widget.FrameLayout@4057f5b0 2131165209
                android.support.v4.app.NoSaveStateFrameLayout@405972e0 -1
                    android.widget.RelativeLayout@40595588 -1
                        android.widget.ImageView@40595978 -1
                        android.widget.Button@40595cd8 -1
                        android.widget.Button@40596448 -1
                        android.widget.Button@40596b78 -1
            android.widget.TextView@4057f8c8 2131165210
            android.widget.FrameLayout@405805a8 2131165211
                android.support.v4.app.NoSaveStateFrameLayout@40592c20 -1
                    android.widget.RelativeLayout@4058e1c0 -1
                        android.view.View@4058e5a8 2131165213
                        com.mycompany.MyCustomWidget@4058e7b8 -1
                        android.widget.TextView@40592158 2131165210
                        android.widget.TextView@405926c0 2131165259
            android.widget.RelativeLayout@40580878 2131165212
                android.view.View@40580cc8 2131165213
                android.widget.FrameLayout@40580e50 2131165214
                android.widget.FrameLayout@40581120 2131165215
            android.widget.LinearLayout@40581440 2131165216
                android.widget.FrameLayout@405816c8 2131165217
                android.widget.FrameLayout@40581930 2131165218
            android.widget.RelativeLayout@40581d00 -1
                android.widget.Button@40582878 2131165261
                android.widget.LinearLayout@40583ac0 -1
                    android.widget.Button@40583d48 2131165262
                    android.widget.Button@405846e0 2131165263
                    android.widget.Button@40585078 2131165264
                    android.widget.Button@40585a10 2131165265
                android.widget.LinearLayout@405863a8 -1
                    android.widget.Button@40586630 2131165266
                    android.widget.Button@40586e50 2131165267
                    android.widget.Button@40587670 2131165268
                    android.widget.Button@40587e90 2131165269

編集2:関連するすべてのXMLは次のとおりです。最初に「フラグメントコンテナ」レイアウト、次に個々のフラグメントのレイアウト。「中央、半分」のコメント以降は、現時点では使用されていませんが、完全を期すために含まれています。

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

    <!--
    Total contents:
    Top bar
    Bottom bar
    Middle area (full screen)
    Middle area (half/half, tablet only)
    Middle area (40/60, tablet only)
    -->

    <!-- Note to self:
    Why are the placeholder FrameLayouts and not just Views?
    Once I get this bootstrapped I should try switching them
    to Views and see if that works just as well.
    -->

    <!-- Top -->
    <FrameLayout
        android:id="@+id/top_fragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        />

    <!-- Bottom -->
    <FrameLayout
        android:id="@+id/bottom_fragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        />

    <TextView
        android:text="This space unintentionally left blank"
        android:textColor="#FFF"
        android:textSize="18sp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        />

    <!-- Center, full -->
    <FrameLayout
        android:id="@+id/center_fragment_full"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/top_fragment"
        android:layout_above="@id/bottom_fragment"
        />

    <!-- Center, halved -->
    <RelativeLayout
        android:id="@+id/center_fragment_half_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/top_fragment"
        android:layout_above="@id/bottom_fragment"
        >

        <View android:id="@+id/keystone"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_height="0dip"
            android:layout_width="0dip"
            android:visibility="invisible"
            />

        <FrameLayout 
            android:id="@+id/center_fragment_half_left"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_toLeftOf="@id/keystone"
            />

        <FrameLayout 
            android:id="@+id/center_fragment_half_right"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_toRightOf="@id/keystone"
            />

    </RelativeLayout>

    <!-- Center, uneven split (roughly 40/60) -->
    <LinearLayout
        android:id="@+id/center_fragment_uneven_split_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/top_fragment"
        android:layout_above="@id/bottom_fragment"
        android:orientation="horizontal"
        >

        <FrameLayout 
            android:id="@+id/center_fragment_uneven_split_left"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_toLeftOf="@id/keystone"
            android:layout_weight="2"
            />

        <FrameLayout 
            android:id="@+id/center_fragment_uneven_split_right"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_toRightOf="@id/keystone"
            android:layout_weight="3"
            />

    </LinearLayout>

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

</RelativeLayout>

これがトップバーの代わりになるレイアウトです。そのIDは問題ありません:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <ImageView
        android:layout_height="@dimen/bars"
        android:layout_width="fill_parent"
        android:background="@drawable/rectangle_1"
        android:layout_alignParentTop="true"
        />
    <Button android:id="@+id/button3"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/button_3"
        android:layout_marginTop="5dip"
        android:layout_marginLeft="4dip"
        />
    <Button android:id="@+id/button4"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/button_4"
        android:layout_marginTop="5dip"
        android:layout_marginRight="4dip"
        />
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:background="@drawable/top_bar1"
        android:text="Title text"
        style="@style/style_2"
        android:gravity="center_horizontal|center_vertical"
        android:layout_marginTop="4dip"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="4dip"
        android:layout_toRightOf="@id/button3"
        android:layout_toLeftOf="@id/button4"
        android:layout_alignParentTop="true"
        android:scaleType="fitXY"
        />
</RelativeLayout>

下部のバーで置き換えられるレイアウトは次のとおりです。そのIDは壊れています:

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

    <ImageView
        android:layout_height="@dimen/bars"
        android:layout_width="fill_parent"
        android:background="@drawable/rectangle_1"
        />
    <Button android="@+id/button1" 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/button_1"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="3dip"
        android:layout_marginTop="5dip"
        style="@style/style_1"
        android:text="foobar"
        />
    <Button android="@+id/button2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/button_2"
        android:layout_alignParentRight="true"
        android:layout_marginRight="3dip"
        android:layout_marginTop="5dip"
        style="@style/style_1"
        android:text="@string/menu"
        />
    <Button android="@+id/button5"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/button_5"
        android:layout_centerHorizontal="true"
        style="@style/style_1"
        android:textSize="14dip"
        android:layout_marginTop="7dip"
        />
</RelativeLayout>

そして最後に、これが私が現在真ん中で置き換えているレイアウトです。そのIDは問題ありません。

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

    <View android:id="@+id/keystone"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_height="0dip"
        android:layout_width="0dip"
        android:visibility="invisible"
        />

    <com.mycompany.MyCustomWidget
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true"
        />

    <TextView
        android:id="@+id/current_location_label"
        android:text="Current Location"
        android:textColor="#FFF"
        android:textSize="13sp"
        android:layout_centerHorizontal="true"
        android:layout_above="@id/keystone"
        android:paddingBottom="5dp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        />

    <TextView
        android:id="@+id/current_location_data"
        android:text="City\nSTATE"
        android:textColor="#ACF"
        android:textSize="22sp"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/keystone"
        android:paddingTop="0dp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        />

</RelativeLayout>
4

1 に答える 1

0

私は問題を見つけました、そしてそれは予想通り非常に愚かなユーザーエラーでした。

android="@+id/button2"

になるはずだった

android:id="@+id/button2"

おっと!みんなありがとう、そして愚かな質問をしてすみません。

于 2012-10-17T20:01:24.700 に答える