我有一个名为 view.xml
的布局,其中包含一个名为 view_related
的 View
。基本上,在我的字典应用程序中,如果条目有相关词,我会将 view_related
替换为 LinearLayout
,其中包含标题“相关条目”和一堆 TextView
表示每个相关词。
每次我在 Activity
中向 LinearLayout
添加 TextView
时,我总是收到 NullPointerException
' s onCreate()
方法,我不明白为什么我的代码看起来很简单。
View .xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="@+id/view_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12pt"
android:textColor="#fff"
android:textStyle="bold"
android:background="#990011"
android:padding="10dp"
android:text="lalala word"
/>
<ScrollView android:id="@+id/view_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/view_header">
<LinearLayout android:id="@+id/view_description_content"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="@string/demo_definition"
/>
<!-- THIS WILL BE REPLACED -->
<View android:id="@+id/view_related"
android:layout_width="match_parent"
android:layout_height="wrap_content"></View>
</LinearLayout>
</ScrollView>
</RelativeLayout>
view_related.xml,LinearLayout
将替换 view.xml 中的 View
元素
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_related_entries"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/view_related_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="#fff"
android:textStyle="bold"
android:textSize="7pt"
android:background="#000"
android:text="Related entries"
/>
<LinearLayout android:id="@+id/view_related_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</LinearLayout>
</LinearLayout>
最后,onCreate
方法,目前假定相关条目是“Hello”和“Goodbye:”
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
LinearLayout ll = (LinearLayout) findViewById(R.id.view_related_list);
TextView tv = new TextView(this);
tv.setText("Hello");
ll.addView(tv); // NULL POINTER EXCEPTION
tv = new TextView(this);
tv.setText("Goodbye");
ll.addView(tv);
LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = findViewById(R.id.view_related);
ViewGroup parent = (ViewGroup) view.getParent();
int index = parent.indexOfChild(view);
parent.removeView(view);
View llview = li.inflate(R.id.view_related_entries, parent, false);
parent.addView(llview, index);
}
最佳答案
setContentView(R.layout.view);
LinearLayout ll = (LinearLayout) findViewById(R.id.view_related_list);
findViewById
函数只查找实际设置的 View 。您当前的 setContentView
没有设置包含您在其中查找的 id 的 xml,因此我引用的第一行不会产生任何结果。
您应该加载正确的 XML,或者使用充气器充气您正在寻找的 View
关于android - 为什么我总是得到对我的 LinearLayout 的空引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13065656/