简体   繁体   English

没有片段显示在ViewPager上

[英]No fragment showing on ViewPager

My ViewPager does not show any fragments even though it load the fragment for my "Review section". 即使ViewPager加载了“查看部分”的片段,我的ViewPager也不显示任何片段。 I used the same code with my "rating section" and it work, even tried redo the ViewPager and adapter but still no progress. 我在“评分部分”中使用了相同的代码,并且可以正常工作,甚至尝试重做ViewPager和适配器,但仍然没有任何进展。 By the way i'm doing this within a fragment. 顺便说一句,我正在片段中做到这一点。

I put a log in the adapter to show whether the fragment is working and also change my Viewpager's background color to check if it's visible or not. 我在适配器中放置了一个日志,以显示该片段是否正常工作,还更改了Viewpager的背景色以查看其是否可见。 Even though the ViewPager is visible but the fragment still does not show. 即使ViewPager是可见的,但片段仍然不显示。

UPDATE: I found out that the problem caused by the activity i use to replace with the fragments. 更新:我发现由我用来替换片段的活动引起的问题。

Here the code snippet (in my activity which hold both "Rating Section" and "review Section". 这里的代码段(在我的活动中同时包含“评分部分”和“评论部分”。

SubSectionFragment_Rating subSectionFragment_rating = new SubSectionFragment_Rating();
    manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.ratingSection,subSectionFragment_rating, subSectionFragment_rating.getTag()).commit();

    SubSectionFragment_ReviewAndInstructions subSectionFragment = new SubSectionFragment_ReviewAndInstructions();
    manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.subTab,subSectionFragment, subSectionFragment.getTag()).commit();

ViewPager is visible ViewPager是可见的

Should display this fragment in ViewPager 应该在ViewPager中显示此片段

Review.xml Review.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hybridelements.openchef.fragment_activities.fragment_subs.SubSectionFragment_ReviewAndInstructions">

<RelativeLayout
    android:id="@+id/main_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tab_layout"
        android:background="@android:color/holo_green_dark"/>

</RelativeLayout>

Review.java (part of the code) Review.java(代码的一部分)

viewPager = (ViewPager) rootView.findViewById(R.id.pager);
    final SubPagerAdapter_ReviewAndInstructions adapter = new SubPagerAdapter_ReviewAndInstructions(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

SubPagerAdapter_ReviewAndInstructions.java SubPagerAdapter_ReviewAndInstructions.java

public SubPagerAdapter_ReviewAndInstructions(FragmentManager fm, int NumOfTabs){
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {
    switch (position){
        case 0:
            ReviewsFragment subTab1 = new ReviewsFragment();
            Log.d("ReviewAndInstruction","ReviewsFragment loaded");
            return subTab1;
        default:
            return null;
    }

}

@Override
public int getCount() {
    return mNumOfTabs;
}

Problem solved thanks to @Yupi for the suggestion to change from getActivity().getSupportFragmentManager() to getChildFragment() . @Yupi提出了将getActivity().getSupportFragmentManager()更改为getChildFragment()的建议,从而解决了问题。 during the problem occurred, when change to getChildFragment() got an error 在发生问题期间,对getChildFragment()更改时出现错误

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.util.SparseArray.get(int)' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.Object android.util.SparseArray.get(int)'

To fix this error, i need to change my activity that start the fragment transaction, so here's the solution to the problem for nested fragments. 要解决此错误,我需要更改启动片段事务的活动,因此这是嵌套片段问题的解决方案。

ViewItem.Java ( BEFORE ) ViewItem.Java( 在之前

FragmentManager manager;

manager = getSupportFragmentManager();

ViewItem.Java ( AFTER ) ViewItem.Java( 之后

FragmentTransaction manager;

manager = getSupportFragmentManager().beginTransaction();

SubPagerFragment_ReviewAndInstructions.java ( BEFORE ) SubPagerFragment_ReviewAndInstructions.java( 在之前

final SubPagerAdapter_ReviewAndInstructions adapter = new SubPagerAdapter_ReviewAndInstructions(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());

SubPagerFragment_ReviewAndInstructions.java ( AFTER ) SubPagerFragment_ReviewAndInstructions.java( 之后

final SubPagerAdapter_ReviewAndInstructions adapter = new SubPagerAdapter_ReviewAndInstructions(getChildFragmentManager(), tabLayout.getTabCount());

Result 结果

Fragment correctly displayed 片段正确显示

In my case, the childFragmentManager and addToBackStack still not fixed my issue, this solution might work instead. 就我而言, childFragmentManageraddToBackStack仍然addToBackStack我的问题, 解决方案可能会起作用。 With combination from @burning-violet solution. 与@ burning-violet解决方案结合使用。

  1. childFragmentManager used to construct the ViewPager childFragmentManager用于构造ViewPager
  2. Using FragmentStatePageAdapter 使用FragmentStatePageAdapter
  3. override restoreState()
  4. Without using addToBackStack(null) // this optional, if you don't want to add to backstack 不使用addToBackStack(null) //此可选,如果您不想添加到backstack

The restoreState() try catch will only silence the error (NPE), it must be fixed accordingly. restoreState() try catch只会使错误(NPE)保持沉默,必须对此进行相应的修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM