2

如何分离或删除 FragmentTransaction?谁能告诉我如何分离它的例子。我试过分离和删除。它仍然无法正常工作。请帮我。请注意,我在代码中使用了 asynctask。

    @Override
        public void onActivityCreated(Bundle savedInstanceState) {

                new loadListView().execute();

            super.onActivityCreated(savedInstanceState);
        }

        public class loadListView extends AsyncTask<Integer, String, String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected String doInBackground(Integer... args) {
                ............
                }   


            @Override
            protected void onPostExecute(String args) {

                if (getActivity() != null) {
                adapter=new MainPageLazyAdapter(getActivity(), menuItems);
                setListAdapter(adapter);



             }
        }


        @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {



    FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(android.R.id.content, this);
        fragmentTransaction.attach(this);
        fragmentTransaction.commit();
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.detach(this);
        fragmentTransaction.commit();



    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }


}

日志猫:

10-09 14:49:42.305: E/AndroidRuntime(5731): FATAL EXCEPTION: main
10-09 14:49:42.305: E/AndroidRuntime(5731): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.actionbarsherlocknavtab/in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity}: java.lang.NullPointerException
10-09 14:49:42.305: E/AndroidRuntime(5731):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at android.os.Looper.loop(Looper.java:137)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at android.app.ActivityThread.main(ActivityThread.java:4512)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at java.lang.reflect.Method.invokeNative(Native Method)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at java.lang.reflect.Method.invoke(Method.java:511)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at dalvik.system.NativeStart.main(Native Method)
10-09 14:49:42.305: E/AndroidRuntime(5731): Caused by: java.lang.NullPointerException
10-09 14:49:42.305: E/AndroidRuntime(5731):     at in.wptrafficanalyzer.actionbarsherlocknavtab.AndroidFragment.onTabSelected(AndroidFragment.java:170)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at com.actionbarsherlock.internal.app.ActionBarWrapper$TabWrapper.onTabSelected(ActionBarWrapper.java:344)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:512)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:441)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:427)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at com.actionbarsherlock.internal.app.ActionBarWrapper.addTab(ActionBarWrapper.java:377)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity.onCreate(MainActivity.java:24)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at android.app.Activity.performCreate(Activity.java:4465)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
10-09 14:49:42.305: E/AndroidRuntime(5731):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
10-09 14:49:42.305: E/AndroidRuntime(5731):     ... 11 more
4

2 回答 2

2

这是错误的,请参阅我的更新。

添加或删除片段后,您需要调用commit() 。FragmentTransaction

例子:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(android.R.id.content, yourFragment);
fragmentTransaction.commit();

更新

根据文档,您不必提交事务,系统会为您完成:http: //developer.android.com/guide/topics/ui/actionbar.html

这是一个运行良好的简约示例。您的活动必须扩展SherlockFragmentActivity(或FragmentActivity)。活动实施TabListener

主要活动:

public class MainActivity extends SherlockFragmentActivity implements TabListener
{
    private ActionBar.Tab homeTab;
    private ActionBar.Tab tagsTab;
    private FragOne fragOne;
    private FragTwo fragTwo;


    @Override
    public void onCreate(Bundle b)
    {
        super.onCreate(b);

        ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        homeTab = bar.newTab().setText("Home");
        tagsTab = bar.newTab().setText("Tags");

        homeTab.setTabListener(this);
        tagsTab.setTabListener(this);

        fragOne = FragOne.getInstance();
        fragTwo = FragTwo.getInstance();

        bar.addTab(homeTab);
        bar.addTab(tagsTab);
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if (tab == homeTab)
            ft.replace(android.R.id.content, fragOne);
        else
            ft.replace(android.R.id.content, fragTwo);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {}

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {}
}

FragOne(另一个片段,,FragTwo只是一个不同的布局):

public class FragOne extends SherlockFragment
{
    private static FragOne instance;


    public static FragOne getInstance()
    {
        if (instance == null)
            instance = new FragOne();
        return instance;
    }

    private FragOne() {}

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.frag_one_layout, container, false);
    }
}
于 2012-10-08T16:06:01.963 回答
0
private void removeFragments()
{
    List<Fragment> fragments = getSupportFragmentManager().getFragments();
    if (fragments != null) {
        android.support.v4.app.FragmentTransaction fragmentTransaction  = getSupportFragmentManager().beginTransaction();
        for (Fragment fragment : fragments) {
            if (fragment instanceof Fragment) {
                fragmentTransaction.remove(fragment);
            }
        }
        fragmentTransaction.commitAllowingStateLoss();
    }
}
于 2018-06-14T05:06:00.750 回答