我想获得对的引用TextView
,但它返回一个null
对象。这是xml
带有布局的文件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/relative1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:id="@+id/textView1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:id="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:id="@+id/textView3" />
</RelativeLayout>
</RelativeLayout>
不要介意缺少对齐等,我删除了与此无关的内容。我尝试按照通常的方式进行操作(我应该提到这是针对列表项的),如下所示:
TextView textView1 = (TextView) rowView.findViewById(R.id.textView1);
TextView textView2 = (TextView) rowView.findViewById(R.id.textView2);
TextView textView2 = (TextView) rowView.findViewById(R.id.textView3);
当这种方法不起作用时,我进行了一些研究,意识到我可能需要从特定的布局中找到它们,如下所示:
RelativeLayout rel = (RelativeLayout) rowView.findViewById(R.id.relative1);
TextView textView1 = (TextView) rel.findViewById(R.id.textView1);
TextView textView2 = (TextView) rel.findViewById(R.id.textView2);
TextView textView2 = (TextView) rel.findViewById(R.id.textView3);
但这也不起作用,而且我似乎找不到其他解决方法。
尝试清理项目...构建->清理项目
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句