简体   繁体   中英

Android SherlockFragmentActivity call SherlockFragment

Originally, I have 2 Activities. A call B activities. Now I changed the A class extends SherlockFragmentActivity and B class extends SherlockFragment.

I tried to changed these in A class:

   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
      }


    }

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

In B class:

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);

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

BUT ERROR. 1. onCreate 2. findViewById..

What i am wrong ?

please give me some hints. Thanks.

I can now display the B fragement but the listview overlap on the main listview..? Why ?

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;
    }

You have to use the onCreateView() method in fragments:

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

        return view;
    }

There you can inflate a View and call findViewById like this:

view.findViewById(...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM