【问题标题】:Fragment does not show up when app starts (onCreate) but does when it resumes (onResume)应用程序启动时(onCreate)不显示片段,但在恢复时(onResume)会显示
【发布时间】:2017-03-05 12:36:06
【问题描述】:

我遇到了一个问题,即我的主要活动中的片段在最初启动应用程序时(在主应用程序的 onCreate 方法期间)没有显示,但在我暂停应用程序然后恢复它时显示(在onResume 方法)

这是我主要活动的重要部分 activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" />               

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

主活动:

public class MainActivity extends Activity {

    protected FrameLayout fragment_container;

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

        if (findViewById(R.id.fragment_container) != null) {
            if (savedInstanceState != null) {
                TextViewFragment fragment = new TextViewFragment();
                fragment.setArguments(getIntent().getExtras());

                getFragmentManager().beginTransaction().add(R.id.fragment_container, fragment).commit();
            }
        }}

TextViewFragment:

public class TextViewFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.textview_fragment, container, false);
    }

}

textview_fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="This is not showing up"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:typeface="sans"
        android:textSize="35sp"/>
</LinearLayout>

感谢任何帮助!

【问题讨论】:

    标签: java android android-fragments


    【解决方案1】:

    当它首先启动时 if (savedInstanceState != null) { 条件为 null。

    您可以删除条件,然后它将始终加载片段。

    【讨论】:

    • 是的,新手错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 2023-04-08
    相关资源
    最近更新 更多