片断中的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
相关文章
相关标签/搜索