English 中文(简体)
设备定向导致例外的碎片标签
原标题:Android Example Code FragmentTabs Causes Exception Upon Device Orientation

我申请时需要使用Tabs,所以我的代码以Android FragmentTabs为例, 发现 here. 。然而,我在代码中更改了几条线,以适应我的目的如下:

在给定的“机器人代码”中,每当用户修改标签时,断面断面的碎片就会被摧毁,断面的碎片就会被创建。为了我的目的,这不可取。虽然我的碎片可以相互独立,但它们会对通过网络连接产生的实时数据作出反应并采取行动。仅仅因为断面是断面的,并不意味着我希望它停止在背景中工作(可悲的是,这似乎是Android的例子中的思维路线 ) 。 因此,我修改了代码以隐藏和显示标签,而不是创建和摧毁标签。

为此,在示例代码I中,将 onTabChanged() 更改为以下两个地方:

public void onTabChanged(String tabId) {
        TabInfo newTab = mTabs.get(tabId);
        if (mLastTab != newTab) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
            if (mLastTab != null) {
                if (mLastTab.fragment != null) {
                    ft.hide(mLastTab.fragment);  //***** 1st Alteration
                    //ft.detach(mLastTab.fragment);
                }
            }
            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(mActivity,
                            newTab.clss.getName(), newTab.args);
                    ft.add(mContainerId, newTab.fragment, newTab.tag);
                } else {
                    ft.show(newTab.fragment);   //************ 2nd Alteration
                    //ft.attach(newTab.fragment);
                }
            }

            mLastTab = newTab;
            ft.commit();
            mActivity.getSupportFragmentManager().executePendingTransactions();
        }
    }

现在,这与我所希望的一样有效。 当设备重新定向时, 问题就会出现。 当第一个方向改变时, 之前选中的标签内容, 即零散部分没有显示 。 这与我隐藏和显示标签的事实有关 。 更严重的是, 在设备第二次调整方向时, 将会丢弃例外 :

E/AndroidRuntime( 371): Caused by: java.lang.NullPointerException E/AndroidRuntime( 371): at android.support.v4.app.FragmentManagerImpl.findFragmentByTag(FragmentManager.java:1211) E/AndroidRuntime( 371): at $TabManager.addTab(ClassName.java:121)

添加 Tab 方法在此 :

public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
        tabSpec.setContent(new DummyTabFactory(mActivity));
        String tag = tabSpec.getTag();

        TabInfo info = new TabInfo(tag, clss, args);

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn t shown.
        FragmentManager fm = mActivity.getSupportFragmentManager();
        Log.i("Class Name","addTab() FragmentManager = " + fm.toString());
        Log.i("Class_Name","addTab() tag = " + tag);
        info.fragment = fm.findFragmentByTag(tag); // **** This causes the exception

        /*
         * Commented the following as Fragments are not removed but hidden/shown on tab change.
         */
        if (info.fragment != null && !info.fragment.isDetached()) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
            ft.detach(info.fragment);
            ft.commit();
        }

        mTabs.put(tag, info);
        mTabHost.addTab(tabSpec);
    }

更具体地说,例外被扔到 info.frafgment = fm.findFrashmentByTag(gag); (gag); 从我试图分离的代码中可以看出,什么物体是无效的,也就是说,是不成块管理器还是标签,但这些标记没有记录为无效。

我并不完全理解Android的例子,如果我附加和分解碎片而不是隐藏和展示它们,这很好。 有人能解释问题是什么吗?

最佳回答

根据< a href="" http:// developmenter.android.com/guide/topics/resources/runtime-changes.html" rel=“nofollow” > 运行时间变化中的机器人 docs , 当方向改变您的活动重新启动时。 这意味着分裂管理器将其状态保存到一个捆包中, 然后重新装入它 。

已经查看过的所有标签都被添加, 即使它们不可见, 也会被隐藏在碎片管理器添加的碎片列表中 。 虽然标签管理器没有存储任何东西, 但是, 它只是被重新创建了, 我怀疑这是新标签管理器与恢复的碎片管理器之间的不匹配。

我建议仔细阅读", http:// developmenter.android.com/guide/topics/resources/runtime-changes.html" rel=“no follow”, 以及一些关于保存/恢复标签的深思熟虑的选择。

其余部分是纯粹的猜测。 它看起来既不是零碎的. incantantitate, 也不是ft. ad. check 看看新碎片是否重复了旧的碎片。 这可能会导致塔布管理者提到一个碎片,而碎片管理者则有两例碎片。

也许足以清除目前无法在Create上看到的标签中的碎片管理器,或者在创建新实例之前先检查碎片是否存在。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签