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

在编程过程中,遇到了一个问题,一直报:

 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)