I am trying to create an ImageView in a Fragment which will refer to the ImageView element which I have created in the XML for the Fragment.
(我试图在一个Fragment中创建一个ImageView,该ImageView引用我在Fragment的XML中创建的ImageView元素。)
However, the findViewById
method only works if I extend an Activity class. (但是,仅当我扩展Activity类时, findViewById
方法才有效。)
Is there anyway of which I can use it in Fragment as well? (无论如何,我也可以在Fragment中使用它吗?)
public class TestClass extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ImageView imageView = (ImageView)findViewById(R.id.my_image);
return inflater.inflate(R.layout.testclassfragment, container, false);
}
}
The findViewById
method has an error on it which states that the method is undefined.
(findViewById
方法上有一个错误,指出该方法未定义。)
ask by simplified. translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…