view.findViewById 和Activity.findViewById区别

来源:互联网 发布:童谣诈骗 知乎 编辑:程序博客网 时间:2024/04/30 07:57

view.findViewById 和Activity.findViewById区别

292人阅读 评论(0)收藏举报
本文章已收录于:
分类:
作者同类文章X
  • android service 一直上传数据 退到后台过几分钟不请求服务器了
  • has leaked ServiceConnection com.baidu.location.LocationClient that was originally bound here 百度地图
  • 百度地图 setScanSpan 无效
  • android content activitynotfoundexception service
  • android 上传图片(压缩) Bitmap 转File
  • 更多
两个类中都有findViewById()方法,Activity中的findViewById最终是调用View中的findViewById方法,这个可以从源码看出来:
[java] view plaincopy
  1. public View findViewById(int id) {  
  2.         return getWindow().findViewById(id);//activity中的方法  
  3.     }  

Activity是先拿到window对象,之后再拿view对象:

[java] view plaincopy
  1. public View findViewById(int id) {  
  2.        return getDecorView().findViewById(id); // window中的方法  
  3.    }  

最后才是调用View中的方法:

[java] view plaincopy
  1. public final View findViewById(int id) {  
  2.         if (id < 0) {  
  3.             return null;  
  4.         }  
  5.         return findViewTraversal(id);         //View中的方法  
  6.     }  

所以,如果在Activity中调用findViewById(int id)的时候,要注意id的来源,如果id不是在当前Activity所在的窗口,拿到的view对象就为空,比如:
[java] view plaincopy
  1. @Override  
  2.     protected void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         setContentView(R.layout.activity_main);  
  5.           
  6.         View contentView = getLayoutInflater().inflate(R.layout.popup, null);  
  7.         final PopupWindow popup = new PopupWindow(contentView, 280360);  
  8.           
  9.         Button button = (Button) findViewById(R.id.bn);  
  10.         button.setOnClickListener(new OnClickListener() {  
  11.             @Override  
  12.             public void onClick(View v) {  
  13.                 popup.showAsDropDown(v);  
  14.             }  
  15.         });  
  16.           
  17.         Button button2 = (Button) findViewById(R.id.close);  
  18.         button2.setOnClickListener(new OnClickListener() {  
  19.               
  20.             @Override  
  21.             public void onClick(View v) {  
  22.                 popup.dismiss();  
  23.             }  
  24.         });  
  25.           
  26.     }  
上面的R.id.close不是在activity_main.xml里面,而是在popup.xml里面(布局文件略),所以拿到的button2为null,要真想拿到button2对象,不能直接调用activity的findViewById()方法,而是调用view的findViewById()方法,改为这样就可以了:
[java] view plaincopy
  1. Button button2 = (Button) contentView.findViewById(R.id.close);  
  2.         button2.setOnClickListener(new OnClickListener() {  
  3.               
  4.             @Override  
  5.             public void onClick(View v) {  
  6.                 popup.dismiss();  
  7.             }  
  8.         }); 



转载:http://blog.csdn.NET/breezylee2009/article/details/38580991

1
0
 
 

我的同类文章

http://blog.csdn.net
  • android service 一直上传数据 退到后台过几分钟不请求服务器了2017-02-22
  • 百度地图 setScanSpan 无效2017-02-21
  • android 上传图片(压缩) Bitmap 转File2017-02-07
  • android 百度地图定位不准问题2016-11-15
  • ExpandableListView点击无法展开子项2016-11-10
  • 解决SD Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)2016-10-05
  • has leaked ServiceConnection com.baidu.location.LocationClient that was originally bound here 百度地图2017-02-21
  • android content activitynotfoundexception service2017-02-21
  • android 百度地图 起点和终点 连线了,变成封闭2016-11-16
  • ExpandableListView 嵌套 ExpandableListView 多出重复数据2016-11-10
  • Android performClick无效2016-11-04
更多文章
0 0