Android SherlockFragmentActivity调用SherlockFragment

时间:2012-12-04 15:52:18

标签: android actionbarsherlock

最初,我有2项活动。一个电话B活动。现在我改变了A类扩展SherlockFragmentActivity和B类扩展了SherlockFragment。

我试图在A班中改变这些:

   private class MyTabListener implements ActionBar.TabListener {
      @Override
      public void onTabSelected(Tab tab, FragmentTransaction ft) {

          if (tab.getPosition()==1) {
              B frag = new B();
              ft.replace(android.R.id.content, frag);

          }

      }

      @Override
      public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
      }

      @Override
      public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
      }


    }

// =============================

在B班:

public class Br extends SherlockFragment{


    /* Called when the activity is first created */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.promotionslist_tableview);



        //get our listview
        ListView myListView = (ListView) findViewById(R.id.myListView_promotionslist_table);

// ==============================

但是错误。 1. onCreate 2. findViewById ..

我错了什么?

请给我一些提示。感谢。

2 个答案:

答案 0 :(得分:2)

我现在可以显示B片段,但列表视图在主列表视图上重叠..? 为什么?

public class B extends SherlockFragment{

    View view; 

    /* Called when the activity is first created */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //setContentView(R.layout.promotionslist_tableview);





        //get our listview
        ListView myListView = (ListView) view.findViewById(R.id.myListView_promotionslist_table);
.
.
.
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.promotionslist_tableview, container, false);
        return view;
    }

答案 1 :(得分:0)

您必须在片段中使用onCreateView()方法:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout, null);

        return view;
    }

在那里你可以膨胀视图并调用findViewById,如下所示:

view.findViewById(...);