AndroidGUI27中findViewById返回null的快速解决办法

在用Eclipse进行Android的界面开发,通过findViewById试图获取界面元素对象时,该方法有时候返回null,造成这种情况主要有以下两种情形。

第一种情形是最普通的。

比如main.xml如下,其中有一个ListView,其id为lv_contactbook

<?xml version="1.0"encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/et_search" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> <ListView android:id="@+id/lv_contactbook" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>

如果在Activity对应的代码中,是这样的写的:

@Override public void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); ListViewlv = (ListView)findViewById(R.id.lv_contactbook); setContentView(R.layout.main); //… }

即在setContentView调用之前,调用了findViewById去找main布局中的界面元素lv_contactbook,那么所得到的lv一定是null。正确的做法是将上面代码中加粗的哪一行,挪至setContentView方法调用之后。

第二种情形。

这种情况下通常是调用LayoutInflater.inflate将布局xml规定的内容转化为相应的对象。比如有rowview.xml布局文件如下(比如在自定义Adapter的时候,用作ListView中的一行的内容的布局):

<?xml version="1.0"encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv_contact_id" android:layout_width="0px" android:layout_height="0px" android:visibility="invisible" android:gravity="center_vertical" /> <TextView android:id="@+id/tv_contactname" android:layout_width="wrap_content" android:layout_height="36dip" android:textSize="16dip" android:layout_marginTop="10dip" android:textColor="#FFFFFFFF" /> </LinearLayout>

假定在自定的Adapter的getView方法中有类似如下的代码:

View rowview = (View)inflater.inflate(R.layout.rowview, parent, false); TextView tv_contact_id =(TextView)rowview.findViewById(R.id.tv_contact_id); TextView tv_contactname =(TextView)rowview.findViewById(R.id.tv_contactname);

有时候居然也会发现rowview非空,但tv_contact_id和tv_contactname都是null!仔细看代码,怎么也看不出错误来。到底是什么原因造成的呢?答案是Eclipse造成的,要解决这个问题,需要这个项目clean一次(Project菜单 -> Clean子菜单),这样就OK了。

第二种情况很隐蔽,因为代码的确没有错。如果一时没有想到解决办法会浪费很多时间。

以上所述是小编给大家介绍的AndroidGUI27中findViewById返回null的快速解决办法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

时间: 2024-08-01 23:57:15

AndroidGUI27中findViewById返回null的快速解决办法的相关文章

AndroidGUI27中findViewById返回null的快速解决办法_Android

 在用Eclipse进行Android的界面开发,通过findViewById试图获取界面元素对象时,该方法有时候返回null,造成这种情况主要有以下两种情形. 第一种情形是最普通的. 比如main.xml如下,其中有一个ListView,其id为lv_contactbook <?xml version="1.0"encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.

Android中findViewById返回为空null的快速解决办法_Android

[问题描述] Android中如下代码: LinearLayout groupPollingAddress = (LinearLayout)findViewById(R.layout.fragment_field_list); 返回为null. [解决过程] 1.参考: android – getActivity().findViewById(R.layout.contacts_list_view) returns null – Stack Overflow AndroidGUI27中findV

Android中findViewById返回为空null的快速解决办法

[问题描述] Android中如下代码: LinearLayout groupPollingAddress = (LinearLayout)findViewById(R.layout.fragment_field_list); 返回为null. [解决过程] 1.参考: android – getActivity().findViewById(R.layout.contacts_list_view) returns null – Stack Overflow AndroidGUI27中findV

PHP JSON返回null或空字符解决办法

原因分析 其它的json_decode($str)返回NULL的一些原因: 1.$str只能UTF-8编码 2.元素最后不能有逗号(与php的array不同) 3.元素不能使用单引号 4.元素值中间不能有空格和\n,必须替换 在用PHP JSON处理符合标准的GBK编码内容时会返回null,将内容从GBK转换为UTF8再处理正常:  代码如下 复制代码 $jsonp = mb_convert_encoding($jsonp, "gbk", "utf-8"); //或

android-为什么自定义图像视图的 findviewbyId 返回 null

问题描述 为什么自定义图像视图的 findviewbyId 返回 null 通过设定zooming 和 boundaruy 限制我可以在屏幕上拖动和缩放 imageview .但是我想应用为多个图像.为此我在 main.xml中使用customview.但问题是findviewbyId 总返回null.问题出在哪里呢? NewzoomlimitActivity .java public class NewzoomlimitActivity extends Activity { /** Calle

iOS9与XCode7中不能使用http连接的快速解决办法_IOS

在Xcode7.0及以上版本中编译iOS APP时,默认会使用iOS9的一项新特性,使得所有http连接被禁用,项目里使用的API没有https支持,就悲剧了.差了官方文档,有这么一段话 App Transport Security App Transport Security (ATS) enforces best practices in the secure connections between an app and its back end. ATS prevents accident

BootStrap 图标icon符号图标glyphicons不正常显示的快速解决办法_javascript技巧

 bootstrap 图标icon符号图标glyphicons不正常显示解决办法如下所示: 分享供各位参考: 1.在ff/http:的地址栏中输入"about:config",即进入配置界面. 2.进入后,搜索"security.fileuri.strict_origin_policy",这是该值应该是true. 3.双击该项,其值自动变为false,即可. 4.修改后,再刷新遇到问题的页面,即可看到正常显示的图标了. 探究问题原因: 1.由于ff/http:一个安

Ajax提交Form表单页面仍会刷新问题的快速解决办法_AJAX相关

搞了半天原来点击提交按钮 少加了个type="button"  原来是  <form >  <button id="sub" />   </form>  结果怎么弄都会刷新页面,加上就好了  PS:form 表单中存在button时ajax提交后自动刷新问题 //在记一个坑(刚学前台,不是很懂,项目中自己给自己挖了个坑,哈哈...) <form class="form-horizontal"> &l

Java enum关键字不识别的快速解决办法_java

从别人那儿拷贝过来的myeclipse java工程,打开一看标红了一大片,仔细一看,原来是不识别enum关键字,这就有点尴尬了. 我自己重新建了一个java工程,测试了下,假如我在新建工程的时候选择JavaSE-1.6: 是识别enum关键字的,但是假如我选择第二个或者默认的jdk1.8,那就不行了,后来发现java compiler compliance level中的最高上限也才1.7,所以,两种解决方法(原理是一样的),一是将compiler compliance level设置到1.5