findViewById 在 <include>d 视图内的 LinearLayout 上返回 null [英] findViewById returns null on a LinearLayout inside an <include>d view

查看:23
本文介绍了findViewById 在 <include>d 视图内的 LinearLayout 上返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚这里的错误.我可能正在寻找一些东西...

I cannot figure out mistake here. I may be oevrlooking some thing...

布局 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <include
        android:id="@+id/headerInclude"
        android:layout_width="fill_parent"
        android:layout_height="38dp"
        android:layout_gravity="top"
        layout="@layout/header" />

    <LinearLayout
        android:id="@+id/actualView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1" >

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:divider="@drawable/dotted_line"
            android:dividerHeight="2dp"
            android:drawSelectorOnTop="false"
            android:paddingBottom="10dp"
            android:paddingTop="10dp" />
    </LinearLayout>

    <include
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:layout_gravity="bottom"
        layout="@layout/footer" />

</LinearLayout>

包含的标头 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/headerLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/header_bgr"
    android:orientation="horizontal"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="7dp" >

    <TextView
        android:id="@+id/tvScreenTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:ellipsize="end"
        android:gravity="center|left"
        android:singleLine="true"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#ffffff"
        android:textSize="17sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvScreenSubTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:gravity="center|right"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:textStyle="bold" />

</LinearLayout>

列出活动java代码:

List Activity java code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    appData = ((DxApp) this.getApplicationContext()).getData();
    setContentView(R.layout.list);
    setTitle("Some Title...");
    // some more code...
    addButtons();
}

protected void addButtons() {
    LinearLayout headerLayout = (LinearLayout) findViewById(R.id.headerLayout);
    Button btn = new Button(this);
    btn.setText("Go to My Sets");
    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            // do some thing here...
        }
    });

    if (headerLayout != null)
        headerLayout.addView(btn);
}

@Override
public void setTitle(CharSequence title) {
    TextView tvTitle = (TextView) findViewById(R.id.tvScreenTitle);
    tvTitle.setText(title);
}

如果我删除检查if(headerLayout != null),我会在headerLayout.addView(btn) 上得到null 指针异常.
我能够在函数 setTitle 中获得 TextView 但有些方法无法获得 LinearLayout

If I remove the check if(headerLayout != null), I get null pointer exception onheaderLayout.addView(btn).
I am able to get TextView in function setTitle but some how cannot get LinearLayout

谁能帮我找出代码有什么问题...

Can any one help me in figuring out what is wrong with the code...

推荐答案

如果您为 include 元素设置了 id 属性,那么这将是膨胀布局的根.因此,与其查找带有 id headerLayoutLinearLayout,不如查找 id headerInclude(或者不要为 headerInclude 设置 id>include 元素).

If you set the id attribute for the include element, then that will be the id for the root of the inflated layout. So, instead of looking for a LinearLayout with the id headerLayout look for the id headerInclude(or don't set an id for the include element).

这篇关于findViewById 在 &lt;include&gt;d 视图内的 LinearLayout 上返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆