SherlockFragment的`onCreateView()`方法没有被调用

时间:2013-10-18 10:20:15

标签: android actionbarsherlock fragment

我是Sherlock lib的新手。我的代码如下

public class MainActivity extends SherlockFragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    com.actionbarsherlock.app.ActionBar actionbar = getSupportActionBar();
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionbar.setTitle("PROFILE");
    ActionBar.Tab frag1tab = actionbar.newTab().setText("LOGIN");
    ActionBar.Tab frag2tab = actionbar.newTab().setText("REGISTER");

    Fragment fragment1 = new Fragment_1();
    Fragment fragment2 = new Fragment_2();

    frag1tab.setTabListener(new MyTabListener(fragment1));
    frag2tab.setTabListener(new MyTabListener(fragment2));

    actionbar.addTab(frag1tab);
    actionbar.addTab(frag2tab);

}

class MyTabListener implements ActionBar.TabListener {
    public Fragment fragment;

    public MyTabListener(Fragment fragment) {
        this.fragment = fragment;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        ft.replace(R.id.fragment_container, fragment);
    }

    @Override
    public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

}

 }

My Fragment代码如下

public class Fragment_1 extends SherlockFragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {


    return inflater.inflate(R.layout.frag1, container,false);

}
}

和我的fragment1.xml代码如下

<EditText
    android:id="@+id/edtxtPersonName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/btnSayHi"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Say Hi" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>

我的活动xml布局如下。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="131dp" >


</FrameLayout>

1 个答案:

答案 0 :(得分:0)

好的问题是你需要将TabListener上的代码从onTabReselected更改为onTabSelected。

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    ft.replace(R.id.fragment_container, fragment);
}

@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

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

@Override
public void onTabSelected(Tab arg0, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    ft.replace(R.id.fragment_container, fragment);

}