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中使用它吗? spa
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
方法上有一个错误,指出该方法未定义。 .net