2012-07-20 74 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