我在 ActionBarSherlock 上遇到 fragment 和 View 问题:
我已经成功实现了 2 个选项卡,这些选项卡打开了我的 fragment View 。
public class AFragment extends SherlockFragment {
private ImageButton imageButton1;
private ImageButton imageButton2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_afragment, container, false);
}
}
这是我的代码,布局显示一个 TextView 和 2 个 ImageButtons。我想声明图像按钮,如 imagebutton = (ImageButton) findViewById(R.id.imagebutton1);但这在 findViewById 处给了我一个错误。我尝试了 AFragment Activity 中的每个位置,但仍然存在相同的错误。我还尝试添加 OnCreate 函数,但这也不起作用......那么我需要将代码放在哪里才能使该 View 工作?谢谢!
最佳答案
您应该声明一个包含您的 View 的变量,进行所需的声明和修改,然后返回 View
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.activity_afragment, container, false);
ImageButton imageButton = (ImageButton) v.findViewById(R.id.imagebutton1);
return v;
}
关于java - Android ActionBarSherlock 选项卡 : Fragments View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11587931/