0

最初,我有 2 个活动。A叫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..

我错了什么?

请给我一些提示。谢谢。

4

2 回答 2

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;
    }
于 2012-12-05T11:07:27.700 回答
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(...);
于 2012-12-04T15:57:08.247 回答