片斷中的findViewById - findViewById in Fragment

問題:

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


解決方案:

參考一: https://stackoom.com/question/RFsY/片斷中的findViewById
參考二: https://oldbug.net/q/RFsY/findViewById-in-Fragment
相關文章
相關標籤/搜索