【问题标题】:Using child from layout that is included multiple times in activity使用活动中多次包含的布局中的子项
【发布时间】:2016-10-16 17:32:01
【问题描述】:

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

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 的活动布局:

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

这是来自活动的一些代码:

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)。知道如何获得重用(包含)布局的子级吗?

【问题讨论】:

    标签: java android-layout nullpointerexception include parent-child


    【解决方案1】:

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

    【讨论】:

    • 好的,它现在可以工作,但现在'myText = (EditText) myInnerLayoutOne.findViewById(R.id.age_edit_text);'被遮住并说:可能会产生'java.lang.NullPointerException'但它现在没有产生它。这是我发现的一篇关于布局的文章,包括link
    • 该警告是正常的。如果您不想看到它,您可以在语句之前执行“断言”或显式检查 null。
    • 感谢您的帮助,我没有足够的声望点来评价您的回答,但让您知道我很感激 :)
    • 没问题。很高兴我能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多