我有一个包含3个标签的应用。选项卡有一个带有按钮的片段,可以在同一个选项卡中转到另一个视图。我的实现很简单。我有视图寻呼机的main_activity。这是main_activity.xml的片段。
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
在MainActivity.java下面
public class MainActivity extends AppCompatActivity {
public SectionsPageAdapter mSectionPageAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
setupViewPager(mViewPager);
//Assigns the ViePager to TabLayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
//Defines the number of tabs by setting appropriate fragment and tab name.
private void setupViewPager(ViewPager viewPager){
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new FirsTab(),"TAB1");
adapter.addFragment(new SecondTab(),"TAB2");
adapter.addFragment(new ThirdTab(),"TAB3");
viewPager.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
当我使用getChildFragmentManager()替换带有FirsTabSubFragment的FirstTab时,我收到“找不到视图”错误。
java.lang.IllegalArgumentException: No view found for id 0x7f0d0073 (com.none.app:id/pager) for fragment FirsTabSubFragment{607b4a6 #0 id=0x7f0d0073}
在FirtTab.java的代码下面
public class FirstTab extends Fragment{
private Button btnScan;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.scan_layout, container, false);
btnScan = (Button) view.findViewById(R.id.btnScan);
btnScan.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
getChildFragmentManager()
.beginTransaction()
.replace(R.id.pager, new FirsTabSubFragment())
.addToBackStack(null)
.commit();
}
});
return view;
}
使用getFragmentmanager()替换会导致空视图。任何人都可以就这个话题向我提出建议吗?