怎么小程序 获取view idEclipse里view的id

Android开发:findViewById返回null的解决办法_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Android开发:findViewById返回null的解决办法
来源:Linux社区&
作者:pathuang68
在用Eclipse进行的界面开发,通过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对应的代码中,是这样的写的:
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了。&
第二种情况很隐蔽,因为代码的确没有错。如果一时没有想到解决办法会浪费很多时间。
相关资讯 & & &
& (11/28/:18)
& (10/10/:25)
& (05月21日)
& (03/08/:13)
& (07/10/:11)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款
匿名 发表于 多谢android新手开发,eclipse环境SDK用的是4.1的, mybutton = (Button)findViewById(R.id.mybutton);_百度知道
android新手开发,eclipse环境SDK用的是4.1的, mybutton = (Button)findViewById(R.id.mybutton);
ndroid新手开发,eclipse环境SDK用的是4.1的,报错错误,button的包已经导入了
Description Resource Path Location Type
Call requires API level 11 (current min is 8): android.app.ActionBar#setDisplayHomeAsUpEnabled Activity_02.java /Activity_02/s...
原因就是api版本低修改的步骤:第一步:修改AndroidManifest.xml里的&uses-sdk android:minSdkVersion=&11& /&第二步:修改project.properties文件里的target=android-11第三步:右键工程--&Android Tools--&Fix project properties完毕
采纳率:39%
你选择的api版本不对。你那个句子是api11才有的 而你选择的是api8
为您推荐:
其他类似问题
android的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)1. Experiment: force specified by the layout editor, the same two button id, and then in the code by findViewById () obtained handle to modify the text after. The results: only one button text changes, and another is not affected. 2. Experiment: the
In this article, we will introduce in two different ways of interaction between the Eclipse RCP View, combined with a trip to the code in detail how the Eclipse View skillfully handle the interaction between. First, explain the effect we want to achi
Android is finally the courage to look at the source code, OK ~ according to the steps given in the Internet: Steps are: 1, first download the SDK source code. android-1.5 download address the following http://rgruet.free.fr/public/android-1.5-cupcak
Such as the title, the previous 1.5 version has already been collated source code, but some people say that you want to reply to 1.6. Just tried it today, all versions of source code, running well. So simply compiled a download link and installation
Such as the title, has been finishing off the previous 1.5 version of the source code, but some people replied that want 1.6. Just tried it today, all versions of source code, running well. So simply compiled a download link and installation method.
Such as the title, the previous 1.5 version has already been collated source code, but some people say that you want to reply to 1.6. Just tried it today, all versions of source code, running well. So simply compiled a download link and installation
Eclipse RCP and Spring Integration Recently on a project like the Eclipse RCP In use Spring, Google a bit in the online information found that relatively few know that Spring Ziji You a Spring-OSGI the project, you can configure Spring in OSGI Servic
Original from: Rain Maple Technical Tutorials Network http://www.fengfly.com Original URL: http://www.fengfly.com/plus/view-.html The menu is a variety of software and development platform will provide the necessary functionality, Eclipse is
1.SpringSource Tool Suite:http://www.springsource.com/developer/sts 2.dbviewer:http://www.ne.jp/asahi/zigen/home/plugin/dbviewer/ 3.EGit:http://download.eclipse.org/egit/updates 4.run-jetty-run:http://run-jetty-run.googlecode.com/svn/trunk/updatesite
我们都是使用xml layout 来去控制UI组件,其实我们也可以完全抛开XML,用纯代码来控制我们的界面UI. 回顾我们学过的,遇到过的UI组件,有容器类的Layout:LinearLayout,RelativeLayout等, 也有视图类UI:TextView, EditText, Button, ImageView等.对应到代码中,我们会发现,他们都是View子类,具体关系如下: Tips:在Eclipse中,可以用Ctrl+T键来查看某个类的类层次关系: ViewGroup 与View
摘 要 孔子云:工欲善其事,必先利其器.作为开发人员,我们致力于为最终用户实现工作流程自动化:然而,许多开发人员却疏忽了将自己的开发流程自动化.企业级应用系统涉及到开发.测试.部署.实施等一系列开发流程,在整个软件开发过程中我们如何借助工具.方法和模式使过程简单自动化,减少一些低效繁琐的开发工作量呢? 本系列文章试图使用一系列的工具的介绍及使用帮助大家在日常工作中减轻繁杂低效的开发工作,借助于现有的一系列工具提升大家的开发效率,使大家日常工作做到事半功倍. 本文作为该系列的第一章,首先介绍大家日
1.安装配置jdk,eclipse和Tomcat 1.1 安装jdk,设置环境变量JAVA_HOME和PATH (设置JAVA_HOME的目的是,许多其他软件都以该变量来查找Java的安装路径,例如Tomcat:设置path目的,可以在任意位置下执行jdk相关命令,如javac.java等) 1.2 环境变量中用户变量和系统变量的区别 一个是当前用户使用,你用其它用户登陆,这个环境变量就不起作用了. 系统变量,是对所有用户都可使用的. 1.3 配置Tomcat环境变量 到官网下载即可,zip结尾
CNF全称:Common Navigator Framework NCE全称:Navigator Content Extensions Eclipse 对于导航视图提供了很完整的实现,如果看过Eclipse其它导航实现,基本上都是使用这个框架来实现的.在官方提供的文档中,有详细的描述和一个简单的小例子.英文好的可以自己翻译来看看具体的详解. 开发中常用的部分: ViewPart 继承 CommonNavigator Navigator Content Extensions 使用扩展点 org.e
[转]Android 步步为营 第5营 代码控制UI,View http://www.cnblogs.com/vivid-stanley/archive//2651399.html 在前面的几讲中,我们都是使用xml layout 来去控制UI组件,其实我们也可以完全抛开XML,用纯代码来控制我们的界面UI. 回顾我们学过的,遇到过的UI组件,有容器类的Layout:LinearLayout,RelativeLayout等, 也有视图类UI:TextView, EditTex
eclipse plug-ins to use svn, the original normal, without any changes in the last few days, when a sudden eclipse in view the file, the file version number behind the document's status icon, and so disappeared. Thought that the plug-in conflicts, uni
-------------------------------------------------- ------------- My Eclipse version: eclipse-rcp-galileo-SR2-win32 Build id: 2 -------------------------------------------------- ------------- Plugin name Version Install or Download Use EM
The emergence of a problem that is not automatically the next eclipse project bin folder is generated class files, this has led to a time of running classNotFoundException: Solution (zz): The solution to the problem summary: 1, view the project -& Bu
1. To BORLAND page to download the latest version of the Together Architect 2006 for Eclipse Note: here when I go to install some detours, just start the download the DESIGNER version, and then follow the online tutorial simply can not be achieved au
mapView the xml &View android: id = &@ + id / mv& android: layout_width = &fill_parent& android: layout_height = &fill_parent& android: layout_weight = &1& android: apiKey = &0yxPVuoWg4JGeXCpA3WaJCnbiR2lLfCC
Eclipse Startup Parameters Transfer from: http://blog.sina.com.cn/s/blog_4f09cc8f0100e5uu.html Eclipse platform is highly configurable. Configuration input in the form of the command line arguments and system attributes set. In many cases, command-li
J2EE Web project in the development, testing process often involves the integration of code and test Web Container. Testing process is usually divided into: development, deployment, testing, modifying four links. Cumbersome to deploy part of which gr
Then realized with a check box on a list of view, this requires a relatively clear understanding Adapter. 1. ArrayAdapter read from the Layout TextView controls, to return to the ListView display, this deal ArrayAdapter for getView method, we can inh
This article outlines how to use the Eclipse platform's built-in debugging features to debug your software projects. Debugging is the programmer's work can not be avoided. There are many ways debugging, but in the final analysis, is to find the code
Access in oracle process of change that id from the original growth can not, insert the data is not successful, &id name=&id& type=&int&& &column name=&DBS_ID& /& &generator /& &/ Id& Open access datab
Transfer from http://www.cnblogs.com/zephyr/archive//1210477.html Recently the client software development process, the use of Eclipse RCP to a number of technical, kind of the feeling of late meet. Despite its development in the daily also
1, view configuration view of the basic information --- --- open event js Brackets in the 'all' value does not work, click on view in the record or records will be taken UNID, if the button event, then all valid 2, the module is of type URL and open
J2EE Web project in the development, testing process often involves the integration of code and test the Web Container. Testing process is usually divided into: development, deployment, test, modify four links. Cumbersome to deploy part of which grea
mysql joint query view to achieve the method instance mysql& mysql& create table employee ( -& Id int, -& First_name varchar (15), -& Last_name varchar (15), -& Start_date date, -& End_date date, -& Salary float (8,2), -& City v
其实,Eclipse是一个可以进行非常灵活配置的系统,除了以缺省的方式启动以外,还可以指定各种参数来定制启动方式. 在参考了一些资料之后,我总结了一些比较常用的启动时Command Arguments,如果有不正确的地方希望大家予以指出. -arch [processor architecture] 描述:指定所使用的处理器的类别 举例:eclipse -arch x86或eclipse -arch sparc -application [id] 描述:指定要运行的应用,id为扩展org.ecl
/** * Performs an AJAX-based update of the list view contents. * @param id string the ID of the list view container * @param options map the AJAX request options (see jQuery.ajax API manual). By default, * the URL to be requested is the one that gene
最近开始学习Hadoop,hadoop 的新版本为了能和各个eclipse 的版本兼容,因此只提供了源代码,开发人员需要手动编译. hadoop eclipse 插件的源代码位于$HADOOP_HOME/src/contrib/eclipse-plugin 下面. 如果只想单独编译hadoop 的ecliose 插件那么需要修改该目录下面的build.xml 文件 下面是我的一个配置文件,大家可以根据自己的环境修改,编译过程需要用到eclipse的目录以及hadoop的lib下面的相关jar文件
一般情况自定义View: 新建一个继承UIView的类后有两个方法:- (id)initWithFrame:(CGRect)frame,在初始化的时候添加 子Views - (void)drawRect:(CGRect)rect 重载它就可以通过代码来自定义View. - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization(初始化) code } r
注意:以下操作基于Hadoop-1.2.1,Eclipse Luna,Win7 64位 1.将hadoop eclipse插件的工程导入Eclipse.插件源码目录如下: D:\Lab\lib\hadoop\hadoop-1.2.1\src\contrib\eclipse-plugin 2.配置工程依赖 3.准备ant的配置文件 D:\Lab\lib\hadoop\hadoop-1.2.1\src\contrib\build-contrib.xml 将以上build-contrib.xml拷贝到
自从学习android的hello world开始 我们就知道了这样一个函数findViewById(),他已经成为了家喻户晓,坑蒙拐骗,杀人越货必备的一个函数(好吧,这句是扯淡) 但一直用也没细致研究过它,直到写程序的时候发现一个由这个函数引起的一个莫名其妙的bug,遂决定好好研究下次函数~ 我们调用的findViewById()函数其实有两种(目前我只看到两种,不确定还有没有其他的),一种是Activity类中findViewById()函数 另外一种是View类中定义的findViewBy
对于需求较为严格一些,应该让ViewPager滑动时,那个指示器也应该滑动才符合逻辑,因此如上代码改造为如下代码 public class AnimActivity extends BaseActivity { private V /** * 指示器偏移宽度 */ private int offsetWidth = 0; private ViewPager mViewP /** * viewPager宽度 */ private int screenWith = 0;
网上有许多介绍Eclipse配置的文章,但大多步骤较多,令人望而却步.在这里,第一部分先介绍Eclipse的安装,第二部分是CDT+MinGW的傻瓜式安装教程.第三部分是CDT+MinGW正统的安装.两种方法没有本质的区别. 1.安装Eclipse和MinGW: 1.1安装Eclipse 要想运行Eclipse,首先需要安装JDK,推荐的是Oracle的JDK SE(原来Java是Sun公司的,后来被Oracle收购了,所以还是去Oracle的网站上下载吧.),地址是http://www.ora
使用 for activity: ViewHelper helper = new ViewHelper(MainActivity.this); helper.id(R.id.text_view).text(&hello world&); helper.id(R.id.button).clicked(new View.OnClickListener() { @Override public void onClick(View v) { } }); helper.id(R.id.image
Android做维护开发的时候,Hierarchy view是非常好用的界面分析工具,对于不是自己开发的界面,可以很方便的查看到view的id,以定位view的相关代码. 但是似乎是Android4.0之后,原先在tool目录的启动方式不能用了,硬要让人使用Android device monitor(ADM). 打开了ADM之后,像原先一样的点击device工具栏的Hierarchy 图标,发现获取出来的view不能查看id.. 不能查看id我还要它干毛.... 放弃了好一段时间后,还是觉得没
Activity assigned to you for the correct permissions, you first need to know what permissions you need to allocate. The correct example is the use of Dialer Activity. Access Dailer Activity managed by CALL_PHONE permission. Through the allocation of
Original Transfer from: https: / / txt.appspot.com/pt2club.blogspot.com/2009/12/gwt-20.html GWT 2.0 Introduction Original URL: http://googlewebtoolkit.blogspot.com/2009/12/introducing-google-web-toolkit-20-now.html Earlier this evening, exciting Camp
Original Transfer from: https: / / txt.appspot.com/pt2club.blogspot.com/2009/12/gwt-20.html GWT 2.0 Introduction Original URL: http://googlewebtoolkit.blogspot.com/2009/12/introducing-google-web-toolkit-20-now.html Earlier this evening, exciting Camp
Linux / UNIX, C + +, Boost, ACE, Shell Linux C + + Advanced Training Course --- Distance * Training Objectives: Linux / UNIX C + + Senior Software Engineer focused on Linux / UNIX server-side software development (background development). Course cove
Activity assigned to you for the correct permissions, you first need to know what you need to assign privileges. Correct example is the use of Dialer Activity. Access Dailer Activity managed by CALL_PHONE permission. Through the allocation of rights,
Job to interview, the interview had to deal with the interview approach. The following questions from me and my friend painful interview experience, raise these issues companies, including IBM, E * Trade, Siebel, Motorola, SUN, and other large and sm
In the RCP program, we often get IWorkbenchPage, if it is from view by, you can use the following method: IWorkbenchPage workbenchPage = getViewSite().getPage(); Sometimes not from the view, but from the outside to obtain IWorkbenchPage, for example,
Linux / UNIX, C + +, Boost, ACE, Shell, Oracle / MySQL ...... Linux / UNIX C + + Advanced Training Course --- Distance * Training Objectives: Linux / UNIX C + + Senior Software Engineer focused on Linux / UNIX server-side software development (backgr
Introduction JasperReports is a powerful-and even more important-well known Open Source Java reporting tool that has the ability to deliver rich content in Formats such as PDF, RTF, HTML, CSV and XML. It is widely used and appreciated in the Java com
Since google 06 Since it entered China on a map, moving developments in the field several times the speed of growth every year basically. Android platform in the latest related applications, if they can understand our google map will be a great help
http://www.huomo.cn/developer/article-b8f0.html 1. Reference documentation 1.1 Obtaining MIPS Android Source Code (http://www.mipsandroid.com/documents/show/1) 1.2 Compiling Android for MIPS (http://www.awakecoding.com/index.php?option=com_content&vi
install when the build failure, prompt: illegal character: \ 65279 -- Resolved Reason: The file encoding problem, eclipse in view the file properties, see the encoding is as follows: Workaround: UltraEdit, EditPlus, etc. Editor
Copyright (C) , All Rights Reserved.
版权所有 闽ICP备号
processed in 0.055 (s). 11 q(s)eclipse找不得listview控件 在java文件里用findviewbyid 方法绑定_百度知道
eclipse找不得listview控件 在java文件里用findviewbyid 方法绑定
了listview
我有更好的答案
1.首先你要确认你的R文件是否是正确的。findviewbyid 方法绑定出现错误,通常都是R文件的错误导致的!查看R引用的是否是当前项目的R文件。2.如果是直接就找不到ListView控件,那可能是你的Sdk包的问题!请检查你的sdk关联是否正确,下载的包是否完整!
为您推荐:
其他类似问题
listview控件的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。

我要回帖

更多关于 android 获取view的id 的文章

 

随机推荐