java - Android:在包含的布局中找不到最外面的 View

标签 java android xml

当前行为

include 标签的内容正在显示,但是,当我尝试在包含的布局中查找外部 View 时,它返回 null。

问题 我做错了什么?

代码

这是 onCreate() 中的 java 代码:

input_box_wrapper = findViewById( R.id.input_box_layout ).findViewById( R.id.input_box_wrapper );

if ( input_box_wrapper == null )
    Log.i( "another", "it's null" ); //this prints out

这是一些 Activity 的 xml 文件中的 include 标签:

<include layout="@layout/msg_input_box" 
         android:id="@+id/input_box_layout"/>

这是 msg_input_box.xml 中包含的 xml 代码:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/input_box_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="5dp"
android:background="@drawable/transparent_background2" >

    <TextView 
    android:id="@+id/send_msg_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="@drawable/transparent_background2"
    android:text="Send" />

    <EditText
    android:id="@+id/input_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@id/send_msg_button"
    android:layout_toLeftOf="@id/send_msg_button"
    android:background="#00000000"
    android:hint="@string/type_a_message"
    android:textColorHint="#EEEEEE"
    android:inputType="textMultiLine"
    android:maxLines="5"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:textColor="#FFFFFF"
    android:textSize="20sp"
    android:shadowColor="#000000"
    android:shadowRadius="3"
    android:shadowDx="3"
    android:shadowDy="3"/> 

</RelativeLayout>

最佳答案

哇,抱歉,但我提出了一个理论,对其进行了测试,并在发布问题后立即意识到了答案。

在包含标签的 id 上使用 findViewById() 返回包含文件的 Root View 。

测试使用:

input_box_wrapper = findViewById( R.id.input_box_layout );

if ( input_box_wrapper == null )
    Log.i( "another", "it's null" );
else
{
    Log.i("another", "not null");

    if ( input_box_wrapper instanceof RelativeLayout )
        Log.i("another", "it's the root view");  
}

输出:

不为空

这是 Root View

关于java - Android:在包含的布局中找不到最外面的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28918780/

相关文章:

javascript - json:如何将 json 字符串转换为 javascript 对象

java - 错误 :Execution failed for task ':app:processDebugAndroidTestResources' . > java.lang.IllegalArgumentException(无错误消息)

java - Android:如何显示带有回调的相机预览?

java - 无法加载 Skuli 库

android - MaterialDatePicker 很慢

xml - 在 Oracle 中解析 SOAP XML

java - 接受输入时无休止的java循环

java - 为什么我在 RecyclerView 上得到一个空引用

android - 如何在android smack 4.2.0-beta1中使用MucEnterConfiguration?

xml - xpath 轴中的命名空间是什么