您的位置:首页 > 大数据 > 人工智能

在MainActivity中调用另外一个布局控件的方法

2017-07-05 15:41 218 查看
在编程过程中,遇到了一个问题,一直报:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.NumberPicker.setMaxValue(int)' on a null object reference

让我头痛。

经过网上查询:

对于非绑定的布局,是不能用findViewById的。

LayoutInflater factory = LayoutInflater.from(当前类.this); 

View layout = factory.inflate(R.layout.你要获取的另一个XML,
null); 

TextView textview = (TextView) layout.findViewById(R.id.控件ID);

一行写完就是:

TextView
textview = (TextView) LayoutInflater.from(当前类.this).inflate(R.layout.你要获取的另一个XML,
null).findViewById(R.id.控件ID);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android
相关文章推荐