Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何确保第一个选项卡始终显示?_Android_Android Fragments_Android Actionbar - Fatal编程技术网

Android 如何确保第一个选项卡始终显示?

Android 如何确保第一个选项卡始终显示?,android,android-fragments,android-actionbar,Android,Android Fragments,Android Actionbar,我有: 仅显示给定URL的WebView的WebViewFragment TabbedWebViewHandler,用于创建包含WebViewFragments的选项卡并将其添加到操作栏 创建并显示4个WebViewFragments的帮助活动(分别用于“进一步信息”、“条款和条件”、“信用”和“此应用的目的”帮助屏幕) 所有这些都可以正常工作,但当HelpActivity首次启动时,第一个选项卡始终只是一个空白的黑屏: 其他选项卡工作正常,如果选择其他选项卡,然后重新选择“信息”选项卡,则第

我有:

  • 仅显示给定URL的WebView的WebViewFragment
  • TabbedWebViewHandler,用于创建包含WebViewFragments的选项卡并将其添加到操作栏
  • 创建并显示4个WebViewFragments的帮助活动(分别用于“进一步信息”、“条款和条件”、“信用”和“此应用的目的”帮助屏幕)
  • 所有这些都可以正常工作,但当HelpActivity首次启动时,第一个选项卡始终只是一个空白的黑屏:

    其他选项卡工作正常,如果选择其他选项卡,然后重新选择“信息”选项卡,则第一个选项卡(在本例中为“信息”选项卡)将正确呈现其web视图

    我总是在创建第一个选项卡后选择它,并使用
    actionBar.selectTab(newTab)将其添加到操作栏选项卡中。我知道执行此操作的代码正在运行,因为日志包含“选择第一个选项卡”

    我还将TabbedWebViewHandler用于执行相同操作的其他活动(包括只有一个“tab”的活动,因此不显示选项卡导航),因此我更愿意修复TabbedWebViewHandler,而不是在HelpActivity中设置解决方法

    如果相关的话,我正在使用ActionBarSherlock/Android支持库来提供我的标签功能

    如何确保活动的第一个选项卡始终正确显示

    WebViewFragment 选项卡式WebViewHandler 帮助活动 日志输出
    在开始添加选项卡之前,将导航模式设置为
    ActionBar.navigation\u mode\u TABS

    编辑: 找到了! 书中有一段话:


    如果工具栏不在
    操作栏中,这基本上可以防止选项卡初始化。导航模式\u选项卡

    我在这里尝试两种方法。1.在添加选项卡之前,将导航模式设置为选项卡。2.在onTabSelected中,使用参数中的片段事务,自己不要调用commit()。我尝试了第二点(使用参数中的片段事务),发现传入的“ft”变量有时为null(至少在第一次加载时)。从这些方法的文档来看,我认为只有在首先调用onTabUnselected()时才会设置:“此选项卡的unselect和新选择的选项卡的select将在单个事务中执行”。我尝试了
    if(ft==null){ft=fragmentManager.beginTransaction();}
    但随后我在android.support.v4.app.FragmentManagerImpl.removeFragment(fragmentManager.java:1165)上得到了一个NullPointerException”。接下来我将尝试第一点……好的,onTabSelected()代码现在更新为:创建一个新的片段事务,然后在没有现有片段事务的情况下提交,或者使用现有片段事务,如果有可用的片段事务,则不提交。请参见上面的(已编辑的)代码。这似乎是可行的(即不会崩溃),但它并不能解决第一个加载的选项卡为空的问题,直到它被取消选择,然后被重新选择。请尝试将WebView设置为WebViewFragment中的私有属性。设置url时,执行
    if(wv!=null)wv.loadUrl(url)@TheoWHtet:好的,我已经这样做了-现在所有的标签都是空白的白色(即没有加载任何内容的网络视图)。setUrl()是从HelpActivity.onCreate()->TWVH.addTab()->TWVH.makeTab()->WVF.setUrl()调用的,HelpActivity.onCreate()发生在WVF.onCreateView()之前(我当前正在加载URL)。这有点脆弱,但是setUrl()总是在片段onCreateView()运行之前被调用。有证据表明,日志始终包含“onCreateView():URL is”file:///android_asset/further_info.html“”(或类似)并且从不包含“onCreateView():URL为“null””非常感谢您的回答Bartosz-特别是感谢您付出了额外的努力并找到了负责的代码。
    
    public class WebViewFragment extends Fragment {
        private static final String TAG = WebViewFragment.class.getSimpleName();
        private String url;
        @Override
        public View onCreateView(
                LayoutInflater inflater,
                ViewGroup container,
                Bundle savedInstanceState) {
            Log.d(TAG, String.format("onCreateView(): URL is '%s'", url));
            View v = inflater.inflate(R.layout.snippet_webview, container, false);
            WebView wv = (WebView) v.findViewById(R.id.webViewWebView);
            wv.loadUrl(url);
            return v;
        }
        public void setUrl(String url) {
            this.url = url;
        }
    }
    
    public class TabbedWebViewHandler {
        private static final String TAG = "TabbedWebViewHandler";
        private final ActionBar actionBar;
        private final Context hostContext;
        private final FragmentManager fragmentManager;
        public TabbedWebViewHandler(SherlockFragmentActivity host) {
            this.hostContext = (Context) host;
            this.actionBar = host.getSupportActionBar();
            this.fragmentManager = host.getSupportFragmentManager();
        }
        public void addTab(String title, String renderUrl) {
            Tab newTab = makeTab(title, renderUrl);
            actionBar.addTab(newTab);
            // FIXME: buggy! tabs don't show on first load
            if (actionBar.getTabCount() == 1) {
                /* first tab: select it by default */
                Log.d(TAG, "Selecting first tab");
                actionBar.selectTab(newTab);
            }
            if (actionBar.getTabCount() > 1) { 
                /* more than one tab: enable navigating between tabs */
                actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            }
        }
        private Tab makeTab(String title, String renderUrl) {
            WebViewFragment f = 
                    (WebViewFragment) 
                        SherlockFragment.instantiate(
                            hostContext, 
                            WebViewFragment.class.getName());
            f.setUrl(renderUrl);
            ActionBar.TabListener l = new WebViewFragmentTabListener(f);
            Tab newTab = actionBar.newTab();
            newTab.setText(title);
            newTab.setTabListener(l);
            return newTab;
        }
        private class WebViewFragmentTabListener implements ActionBar.TabListener {
            private final WebViewFragment fragment;
            public WebViewFragmentTabListener(WebViewFragment f) {
                this.fragment = f;
            }
            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) {
                // replace current fragment with this fragment
                if (ft == null) { 
                    fragmentManager
                        .beginTransaction()
                        .replace(android.R.id.content, fragment)
                        .commit();
                } else {
                    ft.replace(android.R.id.content, fragment);
                }
            }
            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                ft.remove(fragment);
            }
            @Override
            public void onTabReselected(Tab tab, FragmentTransaction ft) {
                // do nothing
            }
        }
    }
    
    public class HelpActivity extends BaseActivity {
        private static final String TAG = "HelpActivity";
        private static final String FURTHER_URL = 
                "file:///android_asset/further_info.html";
        private static final String DISCLAIMER_URL = 
                "file:///android_asset/terms_and_conditions.html";
        private static final String CREDITS_URL = 
                "file:///android_asset/image_credits.html";
        private static final String PURPOSE_URL = 
                "file:///android_asset/purpose.html";
        @Override
        public void onCreate(Bundle savedInstanceState) {
            Log.i(TAG, "onCreate()");
            super.onCreate(savedInstanceState);
            TabbedWebViewHandler twvh = new TabbedWebViewHandler(this);
            twvh.addTab("Info", FURTHER_URL);
            twvh.addTab("Terms", DISCLAIMER_URL);
            twvh.addTab("Credits", CREDITS_URL);
            twvh.addTab("Purpose", PURPOSE_URL);
        }
    
    }
    
    I/HelpActivity(20038): onCreate()
    D/TabbedWebViewHandler(20038): Selecting first tab
    D/WebViewFragment(20038): onCreateView(): URL is file:///android_asset/further_info.html
    I/webclipboard(20038): clipservice: android.sec.clipboard.ClipboardExManager@41d57ff8
    D/WebViewFragment(20038): onCreateView(): URL is file:///android_asset/further_info.html
    I/webclipboard(20038): clipservice: android.sec.clipboard.ClipboardExManager@41d57ff8
    D/WebViewFragment(20038): onCreateView(): URL is file:///android_asset/further_info.html
    I/webclipboard(20038): clipservice: android.sec.clipboard.ClipboardExManager@41d57ff8
    
    if (getNavigationMode() != NAVIGATION_MODE_TABS) {
        mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION;
        return;
    }