我试图在片段中创建一个ImageView,它将引用我在片段的XML中创建的ImageView元素.但是,该findViewById
方法仅在扩展Activity类时才有效.无论如何,我还可以在片段中使用它吗?
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);
}
}
Run Code Online (Sandbox Code Playgroud)
该findViewById
方法有一个错误,表明该方法是未定义的.