`
sxdsly
  • 浏览: 11277 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

findViewById返回Null

阅读更多

错误:findViewById返回Null,报nullpointer错误

网上搜了下,总结原因,一般为一下几种:

 

1.在另一个view的元素应该用baseView.findViewById()来拿

findViewById()是要指定view的,如果在该view下找不到,自然报null。平时注意养成写view.findViewById()的习惯就不容易错了。

 

2.findViewById在setContentView(R.layout.main);之前.

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

 

3. 使用了id的旧风格

通过findViewById()获取一个Button时一直返回null值,百思不得其解。最后发现是因为在layout文件中使用了id的旧风格,如下所示:

<Button id="@+id/btn_ok" />

改成android:id就成功了。

 

<Button android:id="@+id/btn_ok" />

4. 没有重写BottomTab方法 (这个情况还没遇到,网上有人提到这种情况,欢迎验证一下)

自定义View 的时候 必须重写以下方法

 public BottomTab(Context context, AttributeSet attrs) {    super(context, attrs);

}

5.clean一下工程,让ID重新生成

这种情况是调用LayoutInflater.inflate将布局xml规定的内容转化为相应的对象。比如有rowview.xml布局文件(比如在自定义Adapter的时候,用作ListView中的一行的内容的布局),假定在自定的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的快速解决办法

    主要介绍了AndroidGUI27中findViewById返回null的快速解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下

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

    主要介绍了Android中findViewById返回为空null的快速解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下

    android getActivity.findViewById获取ListView 返回NULL的方法

    在控件ID正确的情况下,检查是否在实例化布局...View view = null; @Override @SuppressLint(HandlerLeak) public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    ListView多条目显示

    //得到当期的返回类型 int type = getItemViewType(position); if (convertView == null) { switch (type) { case TYPE_1: convertView = View.inflate(MainActivity.this, R....

    SQLiteDemo

    null, null, null, null, null); return this.convertToPeople(results);//将results传给convertToPeople方法中的形参,并将peoples数组的值返回 } public People_mll[] queryIdData(long id){//定义一个类型...

    getParent()与getRootView()

    从名字上来理解, getParent() 是获得它的父View,如果他没有父View返回null。 举个栗子: 这里我在约束布局里定义了一个ImageView。我使用getParent()看一下结果 view=findViewById(R.id.image) //ImageView 对象...

    百度地图 定位城市或者详细信息

    定位城市或者详细信息百度地图act_person_location = (TextView) findViewById(R.id.act_person_location); mLocationClient = new LocationClient(context.getApplicationContext()); //声明LocationClient类 ...

    安卓记事本

    Cursor c = dbReader.query(TABLE_NAME, null, null, null, null, null, null); c.moveToPosition(position);// 移动光标到一个绝对的位置 Intent i = new Intent(MainActivity.this, ...

    百度location

    if(bdLocation==null||mMapView==null) return; MyLocationData myLocationData= new MyLocationData.Builder().accuracy(bdLocation.getRadius()).direction(100). latitude(bdLocation.getLatitude())....

    开发多媒体播放器

    * 重写返回键功能:返回上一级文件夹 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // 是否触发按键为back键 if (keyCode == KeyEvent.KEYCODE_BACK) { pathEditText = ...

    android 视频处理代码

    //视频返回路径 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.video_layout); startVideo(); } private void...

    目录选择器源代码

    * 点OK按钮返回的结果,目录或者目录+文件名 */ public DirChooserDialog(Context context, int type, String[] fileType, String resultPath) { super(context); this.context = context; this.type = type;...

    Android 模拟的单选列表对话框.rar

     Dialog dialog = null;//声明一个Dialog对象用于返回  switch(id){//对id进行判断  case LIST_DIALOG_SINGLE:  Builder b = new AlertDialog.Builder(this);//创建Builder对象  b.setIcon(R.drawable.header);...

    包含拍照截图修图的安卓程序

    private static final String TAG = null; private String filename; private Uri imageUri; private ImageView showImage; private File outputImage; @Override protected void onCreate(Bundle ...

    高仿微信界面

    mViewPager = (ViewPager) findViewById(R.id.vp_mainvp); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 需要编写一个...

    解决Android SearchView不显示搜索icon的问题

    之前碰到了一个页面展示问题,SearchView放在Toolbar里面,展示在页面顶部,发现进入这个页面后,左上角是箭头图标(表示点击返回),中间区域就是搜索栏,不过看到会显示搜索icon:magnifying_glass_tilted_left:感觉...

Global site tag (gtag.js) - Google Analytics