findViewById()返回null,为什么呢? [英] findViewById() returns Null, Why?
问题描述
我有一个是通过findViewById搜索并保存返回NULL一个TextView。我有一个指向包含比我的TextView的另一个布局类的OnCreate一个的setContentView。如何使它返回返回null好吗?
I have a textView which is searched by findViewById and that keeps returning Null. I have a setContentView on the onCreate of the class that points to a layout other than the one containing my textView. How to make it return none null please ?
下面是我的活动和两个XML文件:
below is my activity and the two xml files :
activity_process.xml:
activity_process.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_process"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/enrollmenttype"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/enrollmenttype_value" />
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_process);
,因为它是一个隐藏的活动我不能编辑activity_process.xml。我需要找到一个方法来过来这一切。
I can't edit the activity_process.xml since it is a hidden activity. I need to find a way to come over all this.
推荐答案
该方法 findViewById
会给你只是你与<$ C设置布局以下儿童的意见$ C>的setContentView 。为什么你想从你的活动不使用布局得到了的TextView
?这是错误的各种方式。
The method findViewById
will give you only views that are children under the layout you set with setContentView
. Why are you trying to get a TextView
from a layout that your activity doesn't use? That is wrong in all sorts of ways.
所以为了让它工作,你必须把那个的TextView
在 activity_process.xml
是这样的:
So in order for it to work, you have to put that TextView
in activity_process.xml
like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_process"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/enrollmenttype"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/enrollmenttype_value">
</TextView>
</LinearLayout>
这篇关于findViewById()返回null,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!