ARG_SECTION_NUMBER不能得到解决 [英] ARG_SECTION_NUMBER cannot be resolved
问题描述
目前,我正在试图创建3种不同的片段,3种不同的标签具有为每个标签不同内容的目的。我卡在我的code一些错误,说:
ARG_SECTION_NUMBER不能解析
块引用>公共类SampleFragmentPagerAdapter扩展FragmentPagerAdapter {
最终诠释PAGE_COUNT = 3;私人字符串tabTitles [] =新的String [] {特色,浏览,继};私人上下文的背景下;公共SampleFragmentPagerAdapter(FragmentManager FM,上下文的背景下){
超(FM);
this.context =背景;
}@覆盖
公众诠释的getCount(){
返回PAGE_COUNT;
}@覆盖
公共片段的getItem(INT位置){
//(原编码)返回PageFragment.newInstance(位置+ 1); //的getItem被称为实例化片段为给定的页面。
//返回一个DummySectionFragment(定义为静态内部类
//下面)附页码为它的独立参数。
开关(位置){
情况下0:
片段片段=新DummySectionFragment();
捆绑ARGS =新包();
args.putInt(fragment.ARG_SECTION_NUMBER,位置+ 1);
fragment.setArguments(参数);
返回片段; 情况1:
片段fragment2 =新DummySectionFragment2();
捆绑args2 =新包();
//args2.putInt(fragment2.ARG_SECTION_NUMBER,位置+ 2);
fragment2.setArguments(args2);
返回fragment2; 案例2: 片段fragment3 =新DummySectionFragment3();
捆绑args3 =新包();
//args3.putInt(fragment3.ARG_SECTION_NUMBER,位置+ 3);
fragment3.setArguments(args3);
返回fragment3;
默认:
返回null;
}}@覆盖
公共CharSequence的getPageTitle(INT位置){
//根据项目位置生成标题
//(原编码)返回tabTitles [位置] 区域设置L = Locale.getDefault();
开关(位置){
情况下0:
返回App.getContext()的getString(R.string.title_section1).toUpperCase(1)。;
情况1:
返回App.getContext()的getString(R.string.title_section2).toUpperCase(1)。;
案例2:
返回App.getContext()的getString(R.string.title_section3).toUpperCase(1)。;
}
返回null;}}
任何人有任何想法,为什么ARGS_SECTION_NUMBER不能
解决
- fragment.ARG_SECTION_NUMBER
- fragment2.ARG_SECTION_NUMBER
- fragment3.ARG_SECTION_NUMBER
的DummySectionFragment.java如下:
公共类DummySectionFragment扩展片段{
/ **
*片段参数重新presenting本节号
*片段。
* /
公共静态最后弦乐ARG_SECTION_NUMBER =section_number_1;
公共DummySectionFragment(){}
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,FALSE);
TextView的dummyTextView =(TextView中)rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments()调用getInt(ARG_SECTION_NUMBER)));
返回rootView;
}}
解决方案这是因为你的片段变量的类型片段。但恒定在DummySectionFragment限定。所以,你需要做的是:
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,位置+ 1);
请记住,当你使用一些类文件中定义的常量,没有必要通过类的实例来访问它们。
I'm currently trying to create 3 different tabs with 3 different fragments for the purpose of having different content for each tab. I'm stuck with some error in my code that says :
ARG_SECTION_NUMBER cannot be resolved
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter { final int PAGE_COUNT = 3; private String tabTitles[] = new String[] { "Featured", "Browse", "Following" }; private Context context; public SampleFragmentPagerAdapter(FragmentManager fm, Context context) { super(fm); this.context = context; } @Override public int getCount() { return PAGE_COUNT; } @Override public Fragment getItem(int position) { //(original coding) return PageFragment.newInstance(position + 1); //getItem is called to instantiate the fragment for the given page. // Return a DummySectionFragment (defined as a static inner class // below) with the page number as its lone argument. switch (position) { case 0: Fragment fragment = new DummySectionFragment(); Bundle args = new Bundle(); args.putInt(fragment.ARG_SECTION_NUMBER, position + 1); fragment.setArguments(args); return fragment; case 1: Fragment fragment2 = new DummySectionFragment2(); Bundle args2 = new Bundle(); //args2.putInt(fragment2.ARG_SECTION_NUMBER, position + 2); fragment2.setArguments(args2); return fragment2; case 2: Fragment fragment3 = new DummySectionFragment3(); Bundle args3 = new Bundle(); //args3.putInt(fragment3.ARG_SECTION_NUMBER, position + 3); fragment3.setArguments(args3); return fragment3; default: return null; } } @Override public CharSequence getPageTitle(int position) { // Generate title based on item position //(original coding) return tabTitles[position]; Locale l = Locale.getDefault(); switch (position) { case 0: return App.getContext().getString(R.string.title_section1).toUpperCase(l); case 1: return App.getContext().getString(R.string.title_section2).toUpperCase(l); case 2: return App.getContext().getString(R.string.title_section3).toUpperCase(l); } return null; }
}
Anyone have any idea why ARGS_SECTION_NUMBER cannot be resolved for
- fragment.ARG_SECTION_NUMBER
- fragment2.ARG_SECTION_NUMBER
- fragment3.ARG_SECTION_NUMBER
The DummySectionFragment.java is as follows:
public class DummySectionFragment extends Fragment { /** * The fragment argument representing the section number for this * fragment. */ public static final String ARG_SECTION_NUMBER = "section_number_1"; public DummySectionFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label); dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER))); return rootView; }
}
解决方案This is because your fragment variable has type Fragment. But constant is defined in DummySectionFragment. So what you need to do is:
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
Remember that when you use constants defined in some class file, there's no need to access them via instance of the class.
这篇关于ARG_SECTION_NUMBER不能得到解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!