我正在尝试使用getString作为我的vIEwpager选项卡标题,所以我可以支持多语言,但每次我使用getString我得到错误片段没有附加到日志中的活动和应用程序强制关闭!
这是我的片段代码
public class RecipesFragment extends Fragment {VIEwPager pager;VIEwPagerAdapter adapter;SlIDingTabLayout tabs;CharSequence[] Titles = {(getResources().getString(R.string.invcraftingcardTitle)), (getResources().getString(R.string.wbcraftingcardTitle)), (getResources().getString(R.string.ovencraftingcardTitle)), (getResources().getString(R.string.paintTitle))};int Numboftabs = 4;@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, @Nullable VIEwGroup container, @Nullable Bundle savedInstanceState) { VIEw v = inflater.inflate(R.layout.recipes_fragment, container, false); return v;}@OverrIDepublic voID onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // Creating The VIEwPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs. adapter = new VIEwPagerAdapter(getActivity().getSupportFragmentManager(), Titles, Numboftabs); // Assigning VIEwPager VIEw and setting the adapter pager = (VIEwPager) getVIEw().findVIEwByID(R.ID.pager); pager.setAdapter(adapter); // Assiging the SlIDing Tab Layout VIEw tabs = (SlIDingTabLayout) getVIEw().findVIEwByID(R.ID.tabs); tabs.setdistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available wIDth // Setting Custom color for the Scroll bar indicator of the Tab VIEw tabs.setCustomTabcolorizer(new SlIDingTabLayout.Tabcolorizer() { @OverrIDe public int getIndicatorcolor(int position) { return getResources().getcolor(R.color.tabsScrollcolor); } }); // Setting the VIEwPager For the SlIDingTabsLayout tabs.setVIEwPager(pager);}
}
解决方法:
这是因为在调用onAttach之后可以使用变量Titles作为getResources
尝试在onAttach方法中初始化它,如下所示:
@OverrIDepublic voID onAttach(Activity activity) { super.onAttach(activity); Titles = {(getResources().getString(R.string.invcraftingcardTitle)), (getResources().getString(R.string.wbcraftingcardTitle)), (getResources().getString(R.string.ovencraftingcardTitle)), (getResources().getString(R.string.paintTitle))};}
总结 以上是内存溢出为你收集整理的java – android Fragment没有附加到activity全部内容,希望文章能够帮你解决java – android Fragment没有附加到activity所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)