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