首页 > 学院 > 开发设计 > 正文

view.findViewById 和Activity.findViewById区别

2019-11-07 23:08:25
字体:
来源:转载
供稿:网友

view.findViewById 和Activity.findViewById区别

2015-02-25 17:10 292人阅读 评论(0)收藏举报本文章已收录于:分类: 作者同类文章Xandroid 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 plaincopypublic View findViewById(int id) {          return getWindow().findViewById(id);//activity中的方法      }  

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

[java] view plaincopypublic View findViewById(int id) {         return getDecorView().findViewById(id); // window中的方法     }  

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

[java] view plaincopypublic final View findViewById(int id) {          if (id < 0) {              return null;          }          return findViewTraversal(id);         //View中的方法      }  所以,如果在Activity中调用findViewById(int id)的时候,要注意id的来源,如果id不是在当前Activity所在的窗口,拿到的view对象就为空,比如:[java] view plaincopy@Override      PRotected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);                    View contentView = getLayoutInflater().inflate(R.layout.popup, null);          final PopupWindow popup = new PopupWindow(contentView, 280, 360);                    Button button = (Button) findViewById(R.id.bn);          button.setOnClickListener(new OnClickListener() {              @Override              public void onClick(View v) {                  popup.showAsDropDown(v);              }          });                    Button button2 = (Button) findViewById(R.id.close);          button2.setOnClickListener(new OnClickListener() {                            @Override              public void onClick(View v) {                  popup.dismiss();              }          });                }  上面的R.id.close不是在activity_main.xml里面,而是在popup.xml里面(布局文件略),所以拿到的button2为null,要真想拿到button2对象,不能直接调用activity的findViewById()方法,而是调用view的findViewById()方法,改为这样就可以了:[java] view plaincopyButton button2 = (Button) contentView.findViewById(R.id.close);          button2.setOnClickListener(new OnClickListener() {                            @Override              public void onClick(View v) {                  popup.dismiss();              }          }); 

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

顶 1 踩 0    上一篇px,dip,dp sp区别下一篇cannot be resolved or is not a field

我的同类文章

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