2012-07-20 69 views
0

我有与片段和观点的问题上ActionBarSherlock:的Android ActionBarSherlock标签:碎片查看

我已经成功地实施2个标签和标签打开我的片段视图。

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个ImageButton。我想声明图像按钮像imagebutton =(ImageButton)findViewById(R.id.imagebutton1);但是这在findViewById中给我一个错误。我尝试了我的AFragment Activity中的每个位置,但仍然存在相同的错误。我也尝试添加一个OnCreate函数,但是这也不起作用...因此,我需要将我的代码用于该视图的工作?谢谢!

回答

1

你应该声明包含您的视图中的变量,你要申报和修改都应,然后返回视图

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; 

} 
+0

WOW!谢谢亲爱的先生,这对我很有用!非常感谢!你救了我4个小时的电谷歌搜索:) (不幸的是我不能投票,因为缺少声誉...) – moritzg 2012-07-20 23:17:24