Java 安卓:Can';在包含的布局中找不到最外面的视图
当前行为 include标记的内容正在显示,但是,当我尝试在include布局中查找外部视图时,它返回null 问题 我做错了什么 代码 下面是Java 安卓:Can';在包含的布局中找不到最外面的视图,java,android,xml,Java,Android,Xml,当前行为 include标记的内容正在显示,但是,当我尝试在include布局中查找外部视图时,它返回null 问题 我做错了什么 代码 下面是onCreate()中的java代码: 以下是某个活动的xml文件中的include标记: <include layout="@layout/msg_input_box" android:id="@+id/input_box_layout"/> 哇,对不起,但我提出了一个理论,测试了它,并在发布问题后立即意识到了答案 对
onCreate()中的java代码
:
以下是某个活动的xml文件中的include标记:
<include layout="@layout/msg_input_box"
android:id="@+id/input_box_layout"/>
哇,对不起,但我提出了一个理论,测试了它,并在发布问题后立即意识到了答案 对include标记的id使用
findViewById()
,返回包含文件的根视图
使用以下工具对其进行测试:
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");
}
输出:
非空
这是根视图
<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>
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");
}