我在主要活动中有一个FrameLayout和导航抽屉。我使用此FrameLayout在Main Activity中处理片段。在这个片段中,我是TabLayout和ViewPager。现在我想要的是在ViewPager片段中的Button上打开一个新片段。但是点击按钮时应用程序崩溃了。是不是因为FrameLayout在Main Activity中而Button是在子片段的子片段中?我已经尝试在Tabs中使用FrameLayout,但这意味着我必须为每个Tab使用FrameLayout而我不想这样做。所以在这方面的任何帮助将不胜感激。另外,如何处理工具栏中的后退箭头单击以返回上一个片段?
public class HomeFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.homefragment, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new TabOne();
case 1 : return new TabTwo();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "ONE";
case 1 :
return "TWO";
}
return null;
}
}
}

public class TabOne extends Fragment {
private Button newFrag;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.tabone, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
newFrag = (Button)view.findViewById(R.id.button);
newFrag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction xfragmentTransaction = getChildFragmentManager().beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new NewFrag()).addToBackStack(null).commit();
}
});
}
}

public class MainActivity extends AppCompatActivity {
DrawerLayout myDrawerLayout;
NavigationView myNavigationView;
Toolbar toolbar;
ActionBarDrawerToggle mDrawerToggle;
FragmentManager myFragmentManager;
FragmentTransaction myFragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
myNavigationView = (NavigationView) findViewById(R.id.nav_drawer) ;
myFragmentManager = getSupportFragmentManager();
myFragmentTransaction = myFragmentManager.beginTransaction();
myFragmentTransaction.replace(R.id.containerView, new HomeFragment()).commit();
myNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem selectedMenuItem) {
myDrawerLayout.closeDrawers();
if (selectedMenuItem.getItemId() == R.id.nav_profile) {
FragmentTransaction fragmentTransaction = myFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new ProfileFragment()).addToBackStack(null).commit();
}
if (selectedMenuItem.getItemId() == R.id.nav_history) {
FragmentTransaction xfragmentTransaction = myFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new HistoryFragment()).addToBackStack(null).commit();
}
return false;
}
});
toolbar = (Toolbar)findViewById(R.id.toolbar);
mDrawerToggle = new ActionBarDrawerToggle(this, myDrawerLayout, toolbar, R.string.app_name, R.string.app_name);
myDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}

FATAL EXCEPTION: main
Process: com.sam.demonavigation, PID: 8523
java.lang.IllegalArgumentException: No view found for id 0x7f0c006b (com.sam.demonavigation:id/containerView) for fragment HistoryFragment{42016928 #0 id=0x7f0c006b}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5388)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:655)
at dalvik.system.NativeStart.main(Native Method)

答案 0 :(得分:0)
这link证明非常有用。使用我的链接中提到的方法,我能够在我的主要活动中使用FrameLayout打开任何片段
// This is the button click event in one of the TabLayout fragment
newFrag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment var1 = new NewFrag();
((MainActivity)getActivity()).replaceFragment(var1);
}
});

// Add this method in MainActivity
public void replaceFragment(Fragment fragment){
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.containerView, fragment).addToBackStack(null);
t.commit();
}

答案 1 :(得分:0)
1-Create ViewPager object
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(3);
setupViewPager(viewPager);
2-then add Custom FragmentPagerAdapter
ViewPagerAdapter adapter = new ViewPagerAdapter(this);
adapter.addFrag(new Fragment1(), "Item1");
adapter.addFrag(new Fragment2(), "Item2");
adapter.addFrag(new Fragment3(), "Item3");
viewPager.setAdapter(adapter);
3-After that write custom adapter definition
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}