2

次のコードを使用して、インターネットからデータを取得して開きます。それはかなりうまく動作しますが、タブを変更して古いタブに戻るとエラーが発生します。たとえば、開始時にタブ 1 からタブ 2 に移動するとうまくいきますが、タブ 1 に戻るとエラーが発生します

public class MainMenu extends FragmentActivity 
{
    private TabHost tHost;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
                     WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main_menu);

        tHost = (TabHost) findViewById(android.R.id.tabhost);
        tHost.setup();

        /** Defining Tab Change Listener event. This is invoked when tab is changed */
        TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() 
        {
            @Override
            public void onTabChanged(String tabId) 
            {
                FragmentManager fm =   getSupportFragmentManager();
                PrayerTimes prayerFragment = (PrayerTimes) fm.findFragmentByTag("prayerTimes");
                CompassActivity qiblaFragment = (CompassActivity) fm.findFragmentByTag("qibla");
                Settings settingFragment = (Settings) fm.findFragmentByTag("settings");


                FragmentTransaction ft = fm.beginTransaction();

                /** Detaches the androidfragment if exists */
                if(prayerFragment!=null)
                    ft.detach(prayerFragment);
                if(qiblaFragment!=null)
                    ft.detach(qiblaFragment);
                if(settingFragment!=null)
                    ft.detach(qiblaFragment);

                /** If current tab is android */
                if(tabId.equals("prayerTimes"))
                { 
                    if(prayerFragment == null)
                    {
                        /** Create AndroidFragment and adding to fragmenttransaction */
                        ft.add(R.id.realtabcontent,new PrayerTimes(), "prayerTimes");
                    }
                    else
                    {
                        /** Bring to the front, if already exists in the fragmenttransaction */
                        ft.attach(prayerFragment);
                    }

                }
                else if (tabId.equals("qibla"))
                {
                    if( qiblaFragment == null)
                    {
                        /** Create AppleFragment and adding to fragmenttransaction */
                        ft.add(R.id.realtabcontent,new CompassActivity(), "qibla");
                     }
                    else
                    {
                        /** Bring to the front, if already exists in the fragmenttransaction */
                        ft.attach(qiblaFragment);
                    }
                }
                else if(tabId.equals("settings"))
                {
                    if( settingFragment == null)
                    {
                        /** Create AppleFragment and adding to fragmenttransaction */
                        ft.add(R.id.realtabcontent,new Settings(), "settings");
                     }
                    else
                    {
                        /** Bring to the front, if already exists in the fragmenttransaction */
                        ft.attach(settingFragment);
                    }                   
                }
                ft.commit();
            }
        };

        /** Setting tabchangelistener for the tab */
        tHost.setOnTabChangedListener(tabChangeListener);

        /** Defining tab builder for Andriod tab */
        TabHost.TabSpec tSpecPrayer = tHost.newTabSpec("prayerTimes");
        tSpecPrayer.setIndicator("Prayer Times");
        tSpecPrayer.setContent(new MyTabContent(getBaseContext()));
        tHost.addTab(tSpecPrayer);

        /** Defining tab builder for Apple tab */
        TabHost.TabSpec tSpecQibla = tHost.newTabSpec("qibla");
        tSpecQibla.setIndicator("Qibla");
        tSpecQibla.setContent(new MyTabContent(getBaseContext()));
        tHost.addTab(tSpecQibla);

        TabHost.TabSpec tSpecSettings = tHost.newTabSpec("settings");
        tSpecSettings.setIndicator("Settings");
        tSpecSettings.setContent(new MyTabContent(getBaseContext()));
        tHost.addTab(tSpecSettings); 
    }
}

エラーは

2-11 12:21:28.245: E/AndroidRuntime(992): FATAL EXCEPTION: main
02-11 12:21:28.245: E/AndroidRuntime(992): java.lang.IllegalStateException: Recursive entry to executePendingTransactions
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:523)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:495)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1221)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.view.View.dispatchRestoreInstanceState(View.java:12088)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2582)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2588)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.view.View.restoreHierarchyState(View.java:12066)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.support.v4.app.Fragment.restoreViewState(Fragment.java:417)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:933)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1264)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.os.Handler.handleCallback(Handler.java:725)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.os.Looper.loop(Looper.java:137)
02-11 12:21:28.245: E/AndroidRuntime(992):  at android.app.ActivityThread.main(ActivityThread.java:5039)
02-11 12:21:28.245: E/AndroidRuntime(992):  at java.lang.reflect.Method.invokeNative(Native Method)
02-11 12:21:28.245: E/AndroidRuntime(992):  at java.lang.reflect.Method.invoke(Method.java:511)
02-11 12:21:28.245: E/AndroidRuntime(992):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-11 12:21:28.245: E/AndroidRuntime(992):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-11 12:21:28.245: E/AndroidRuntime(992):  at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

0

問題は、あるフラグメントを別のフラグメント内に作成できないことです。これにより、エラーが発生します。幸いなことに、最新のアップデートにより、これ可能になり、許可されるようになりました。これを行う方法の詳細については、Android のドキュメントを参照してください。Android-Support ライブラリの使用を検討してください。

http://developer.android.com/about/versions/jelly-bean.html#42-ui-tools

于 2013-02-11T12:40:38.897 に答える