Java TabbedActivity随机显示片段,但大部分时间为空

Java TabbedActivity随机显示片段,但大部分时间为空,java,android,Java,Android,我用一些片段创建了一个TabbedActivity,这些片段有时加载,有时不加载,我到处搜索,但找不到问题 这是我的主要活动: public class TwitterActivity extends AppCompatActivity implements MessagesFragment.OnFragmentInteractionListener, NotificationFragment.OnFragmentInteractionListener,

我用一些片段创建了一个TabbedActivity,这些片段有时加载,有时不加载,我到处搜索,但找不到问题

这是我的主要活动:

public class TwitterActivity extends AppCompatActivity implements MessagesFragment.OnFragmentInteractionListener, NotificationFragment.OnFragmentInteractionListener,
                                                                ProfileFragment.OnFragmentInteractionListener, TimeLineFragment.OnFragmentInteractionListener {

private static final String APP_TAG = "Twitter";

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    setContentView(R.layout.activity_twitter);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    final TextView toolbarTitle = toolbar.findViewById(R.id.toolbar_title);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);

    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(3);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int position = tab.getPosition();

            if(position == 0){
                toolbarTitle.setText(R.string.tab_timeline);
            }else if(position == 1){
                toolbarTitle.setText(R.string.tab_messages);
            }else if(position == 2){
                toolbarTitle.setText(R.string.tab_notifications);
            }else if(position == 3){
                toolbarTitle.setText(R.string.tab_profile);
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) { }

        @Override
        public void onTabReselected(TabLayout.Tab tab) { }
    });
    tabLayout.setupWithViewPager(mViewPager, false);

}

@Override
public void onFragmentInteraction(Uri uri) { }

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        Log.d(APP_TAG, String.valueOf(position));

        if(position == 0){
            return TimeLineFragment.newInstance();
        }else if(position == 1){
            return MessagesFragment.newInstance();
        }else if(position == 2){
            return NotificationFragment.newInstance();
        }else if(position == 3){
            return ProfileFragment.newInstance();
        }
        return null;
    }

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

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            case 0:
                return getString(R.string.tab_timeline);
            case 1:
                return getString(R.string.tab_messages);
            case 2:
                return getString(R.string.tab_notifications);
            case 3:
                return getString(R.string.tab_profile);
            default:
                return getString(R.string.tab_timeline);
        }
    }

}

}
public class TimeLineFragment extends Fragment {

private OnFragmentInteractionListener mListener;

public TimeLineFragment() {
}

public static TimeLineFragment newInstance() {
    TimeLineFragment fragment = new TimeLineFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_time_line, container, false);
}

public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    void onFragmentInteraction(Uri uri);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activities.TwitterActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:src="@mipmap/ic_launcher"
                />

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="@string/tab_timeline"
                android:textSize="18sp"
                android:textStyle="bold" />

        </RelativeLayout>

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:elevation="80dp"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        app:tabGravity="fill"
        app:tabIndicatorColor="@android:color/holo_orange_dark"
        app:tabIndicatorHeight="0dp"
        app:tabMode="fixed">

    </android.support.design.widget.TabLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_above="@+id/tabs"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>
这是我的一个片段(它们都是一样的):

public class TwitterActivity extends AppCompatActivity implements MessagesFragment.OnFragmentInteractionListener, NotificationFragment.OnFragmentInteractionListener,
                                                                ProfileFragment.OnFragmentInteractionListener, TimeLineFragment.OnFragmentInteractionListener {

private static final String APP_TAG = "Twitter";

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    setContentView(R.layout.activity_twitter);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    final TextView toolbarTitle = toolbar.findViewById(R.id.toolbar_title);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);

    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(3);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int position = tab.getPosition();

            if(position == 0){
                toolbarTitle.setText(R.string.tab_timeline);
            }else if(position == 1){
                toolbarTitle.setText(R.string.tab_messages);
            }else if(position == 2){
                toolbarTitle.setText(R.string.tab_notifications);
            }else if(position == 3){
                toolbarTitle.setText(R.string.tab_profile);
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) { }

        @Override
        public void onTabReselected(TabLayout.Tab tab) { }
    });
    tabLayout.setupWithViewPager(mViewPager, false);

}

@Override
public void onFragmentInteraction(Uri uri) { }

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        Log.d(APP_TAG, String.valueOf(position));

        if(position == 0){
            return TimeLineFragment.newInstance();
        }else if(position == 1){
            return MessagesFragment.newInstance();
        }else if(position == 2){
            return NotificationFragment.newInstance();
        }else if(position == 3){
            return ProfileFragment.newInstance();
        }
        return null;
    }

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

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            case 0:
                return getString(R.string.tab_timeline);
            case 1:
                return getString(R.string.tab_messages);
            case 2:
                return getString(R.string.tab_notifications);
            case 3:
                return getString(R.string.tab_profile);
            default:
                return getString(R.string.tab_timeline);
        }
    }

}

}
public class TimeLineFragment extends Fragment {

private OnFragmentInteractionListener mListener;

public TimeLineFragment() {
}

public static TimeLineFragment newInstance() {
    TimeLineFragment fragment = new TimeLineFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_time_line, container, false);
}

public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    void onFragmentInteraction(Uri uri);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activities.TwitterActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:src="@mipmap/ic_launcher"
                />

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="@string/tab_timeline"
                android:textSize="18sp"
                android:textStyle="bold" />

        </RelativeLayout>

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:elevation="80dp"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        app:tabGravity="fill"
        app:tabIndicatorColor="@android:color/holo_orange_dark"
        app:tabIndicatorHeight="0dp"
        app:tabMode="fixed">

    </android.support.design.widget.TabLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_above="@+id/tabs"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>
这是我的主要活动的布局文件:

public class TwitterActivity extends AppCompatActivity implements MessagesFragment.OnFragmentInteractionListener, NotificationFragment.OnFragmentInteractionListener,
                                                                ProfileFragment.OnFragmentInteractionListener, TimeLineFragment.OnFragmentInteractionListener {

private static final String APP_TAG = "Twitter";

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    setContentView(R.layout.activity_twitter);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    final TextView toolbarTitle = toolbar.findViewById(R.id.toolbar_title);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);

    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(3);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int position = tab.getPosition();

            if(position == 0){
                toolbarTitle.setText(R.string.tab_timeline);
            }else if(position == 1){
                toolbarTitle.setText(R.string.tab_messages);
            }else if(position == 2){
                toolbarTitle.setText(R.string.tab_notifications);
            }else if(position == 3){
                toolbarTitle.setText(R.string.tab_profile);
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) { }

        @Override
        public void onTabReselected(TabLayout.Tab tab) { }
    });
    tabLayout.setupWithViewPager(mViewPager, false);

}

@Override
public void onFragmentInteraction(Uri uri) { }

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        Log.d(APP_TAG, String.valueOf(position));

        if(position == 0){
            return TimeLineFragment.newInstance();
        }else if(position == 1){
            return MessagesFragment.newInstance();
        }else if(position == 2){
            return NotificationFragment.newInstance();
        }else if(position == 3){
            return ProfileFragment.newInstance();
        }
        return null;
    }

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

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            case 0:
                return getString(R.string.tab_timeline);
            case 1:
                return getString(R.string.tab_messages);
            case 2:
                return getString(R.string.tab_notifications);
            case 3:
                return getString(R.string.tab_profile);
            default:
                return getString(R.string.tab_timeline);
        }
    }

}

}
public class TimeLineFragment extends Fragment {

private OnFragmentInteractionListener mListener;

public TimeLineFragment() {
}

public static TimeLineFragment newInstance() {
    TimeLineFragment fragment = new TimeLineFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_time_line, container, false);
}

public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    void onFragmentInteraction(Uri uri);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activities.TwitterActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:src="@mipmap/ic_launcher"
                />

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="@string/tab_timeline"
                android:textSize="18sp"
                android:textStyle="bold" />

        </RelativeLayout>

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:elevation="80dp"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        app:tabGravity="fill"
        app:tabIndicatorColor="@android:color/holo_orange_dark"
        app:tabIndicatorHeight="0dp"
        app:tabMode="fixed">

    </android.support.design.widget.TabLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_above="@+id/tabs"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

请帮帮我


编辑:删除了无用的选项卡项。

您可能必须使用getChildFragmentManager()而不是getSupportFragmentManager()作为参数来创建适配器

适配器是在主活动中创建的,我无法访问活动中的getChildFragmentManager()。为什么xml中有3个tabItems?删除将由适配器添加的项如果你是对的,我将它们从我的代码和帖子中删除。但仍无法解决此问题。请尝试使用
FragmentStatePagerAdapter
而不是
FragmentPagerAdapter
。。它应该可以修复你的问题。如果你的问题不起作用,碎片仍然会随机出现空白。如果你在方向改变后在适配器中记录返回的项目,你会得到什么?