使用 activity 中多次包含的布局中的子项

Using child from layout that is included multiple times in activity

这是我想在 activity 中多次重复使用的布局:

reusable_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="50dp"
    android:background="#30000000"
    android:id="@+id/layout_root">

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

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Age :"
            android:id="@+id/textView5"
            android:layout_weight="1"
            android:gravity="center"
            android:textAppearance="@style/AppTheme.Text"
            android:layout_gravity="center" />

        <View
            android:layout_width="@dimen/separator"
            android:layout_height="50dp"
            android:id="@+id/view"
            android:background="@android:color/white"
            android:layout_gravity="center" />

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/age_edit_text"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_gravity="center"
            android:inputType="numberDecimal"
            android:maxLines="1"
            android:textSize="@dimen/text_size"
            android:textColor="@android:color/white"
            android:maxLength="5"/>
    </LinearLayout>

</LinearLayout>

这是我要多次包含 reusable_layout.xml 的 activity 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@mipmap/bsc_odds">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/reusable_layout"
            android:id="@+id/rl_1" />

        <include
            layout="@layout/separator_hor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <include
            layout="@layout/reusable_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/rl_2" />

        <include
            layout="@layout/separator_hor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <include
            layout="@layout/reusable_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/rl_3" />

        <include
            layout="@layout/separator_hor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <include
            layout="@layout/reusable_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/rl_4" />

        <include
            layout="@layout/separator_hor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <include
            layout="@layout/reusable_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/rl_5" />

        <include
            layout="@layout/separator_hor_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/gender" />

        <include
            layout="@layout/separator_hor_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <include
            layout="@layout/separator_hor_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <include
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            layout="@layout/result"
            android:id="@+id/result" />
    </LinearLayout>

</LinearLayout>

这是来自 activity 的一些代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

myRootLayout = (LinearLayout) findViewById(R.id.layout_root);
// Next line is shadowed and says: may produce 'java.lang.NullPointerException'
myInnerLayoutOne = (LinearLayout) myRootLayout.findViewById(R.id.rl_1); 

myText = (EditText) myInnerLayoutOne.findViewById(R.id.age_edit_text);
...}

启动 activity 时,应用程序崩溃 (java.lang.NullPointerException)。知道如何获得重用(包含)布局的子级吗?

ID R.id.layout_root 正在被 R.id.r1_1 等取代。请尝试以下操作:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

myInnerLayoutOne = (LinearLayout) findViewById(R.id.rl_1); 

myText = (EditText) myInnerLayoutOne.findViewById(R.id.age_edit_text);

...}