Android 无法在基于单个layout.xml文件的两个不同片段中使用findViewById检索正确的TextView
我正在Android 无法在基于单个layout.xml文件的两个不同片段中使用findViewById检索正确的TextView,android,android-fragments,android-viewpager,fragment,Android,Android Fragments,Android Viewpager,Fragment,我正在main活动中使用FragmentPagerAdapter。我使用res\layout\fragment\u text.xml文件创建两个片段,每个片段使用用户输入的不同数据。当我尝试findViewById()时,它会从不同的片段返回数据。以下是适用的代码: //this is from my public class SectionsPagerAdapter extends FragmentPagerAdapter @Override public Frag
main活动中使用FragmentPagerAdapter
。我使用res\layout\fragment\u text.xml
文件创建两个片段,每个片段使用用户输入的不同数据。当我尝试findViewById()
时,它会从不同的片段返回数据。以下是适用的代码:
//this is from my public class SectionsPagerAdapter extends FragmentPagerAdapter
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
switch (position){
case 0: return MultipleChoiceSingleAnswerFragment.newInstance(position + 1);
**case 1: return TextFragment.newInstance("1", "2");**
**case 2: return TextFragment.newInstance("10", "20");**
case 3: return ScoreFragment.newInstance("100", "200");
default: return null;
}
}
//here I collect the fragment tags as they are being instantiated
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment newFragment = (Fragment) super.instantiateItem(container, position);
// save the appropriate fragment tag depending on position
fragmentTags[position] = newFragment.getTag();//store in array of Strings
return newFragment;
}
在onclick
监听器中,我尝试检索片段,并使用toast将用户输入的文本显示到EditText
视图中。此代码在另一个片段中调用,该片段只有一个TextView
和一个按钮
public void onScorePressed(){
int pageCount = mSectionsPagerAdapter.getCount();
**TextFragment textfragment = (TextFragment) getSupportFragmentManager().findFragmentByTag(fragmentTags[1]);
CharSequence text = textfragment.getAnswer()**;<<<<<main problem in call,see below
int duration = Toast.LENGTH_SHORT;
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
getAnswer方法返回
“mParam1=1文本\u答案=输入的文本”
来自上面案例2中实例化的第二个TextFragment。这表明我实际上得到了正确的TextFragment,因为mParam1与实例化中传递的参数匹配。我需要一种更好的方法来获取EditText视图的文本。感谢您的帮助。谢谢
我在研究中使用的帖子:
您没有为片段设置标记,因此无法按标记检索片段。据我所知,片段标记只能在XML布局中设置,或者在使用事务的add或replace方法创建片段时设置。我希望在中使用NullPointerExection
fragmentTags[position] = newFragment.getTag();
因为之前没有设置标记
作为解决方案,我建议使用一个数组来保存对片段的引用,而不是一个标签数组:
Fragment[] fragments = new Fragment[NUMBER_OF_FRAGMENTS];
因此,getItem方法可以如下所示:
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
Fragment fragment;
switch (position){
case 0:
fragment = MultipleChoiceSingleAnswerFragment.newInstance(position + 1);
break;
case 1:
fragment = TextFragment.newInstance("1", "2");
break;
case 2:
fragment = TextFragment.newInstance("10", "20");
break;
case 3:
fragment = ScoreFragment.newInstance("100", "200");
default:
fragment = null;
}
fragments[position]=fragment;
return fragment;
}
然后您可以完全省略实例化Item方法,因为它不是必需的
最后,在onscorespressed方法中,通过
TextFragment textfragment = fragments[i];
希望这有帮助。无需使用getFragmentByTag()
,只需保留对InstanceItem()
覆盖中获得的片段的引用。这不是我问题的答案。我没有得到空指针异常。我得到了一个碎片,但它是错误的。我的代码使用1 layout.xml文件创建具有相同布局但数据不同的两个页面/片段。无论使用哪个标记,我都只能检索第二个实例。我刚刚尝试了中提到的方法,但仍然得到相同的结果。两个片段都实例化了第二个片段中的数据。在TextFragment中显示newInstance()
方法。看起来您可能只创建了TextFragment的一个实例,而您应该有多个实例。@DanielNugent我肯定有两个片段,因为我可以更改索引以获得第二个片段。newInstance()只创建一个片段,但它被调用了两次<代码>公共静态TextFragment newInstance(String param1,String param2){TextFragment fragment=new TextFragment();Bundle args=new Bundle();args.putString(ARG_param1,param1);args.putString(ARG_param2,param2);fragment.setArguments(args);return fragment;}
请查看我的编辑。我得到了正确的碎片。问题是当我尝试使用getViewById从EditText视图获取文本时。另外,我按照你的建议做了,但仍然存在与前面提到的相同的问题。谢谢你的意见。如果有时间,请再次查看我的帖子。如果您只需要EditText中的文本,为什么不直接返回txt.getText(),而不是“mParam1=“+mParam1+”text\u answer=“+txt.getText()”?返回mParam只是一种调试技术。它表明我的问题不是getFragmentByTag,而是使用getViewById获取片段中的文本。至少我是这么想的。它今天开始工作了,我说不出原因。谢谢你的意见。
TextFragment textfragment = fragments[i];