简体   繁体   English

为什么我的片段空白?

[英]Why is my Fragment Blank?

Heyy all! 大家好! I'm trying to learn fragments while coding, and I'm attempting to switch one fragment with another. 我正在尝试在编码时学习片段,并且试图将一个片段与另一个片段交换。 Whenever I click the next button, the current fragment is replaced with a blank one and I have no idea why! 每当我单击下一个按钮时,当前片段就会被替换为空白片段,我也不知道为什么!

MainActivity.Java: MainActivity.Java:

package com.alexoladele.testingshit;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import layout.WelcomeScreenFragment;

public class MainActivity extends AppCompatActivity {

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

//        Makes sure that the root_layout view is not null before doing anything
        if (findViewById(R.id.root_layout) != null) {

//            Makes sure that there's no saved instance before proceeding
            if (savedInstanceState == null) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction manager = fm.beginTransaction();
                manager
                        .add(R.id.root_layout, WelcomeScreenFragment.newInstance(), "welcomeScreen")
                        .commit();
            }
        }


    }
}

activity_main.xml: activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.alexoladele.testingshit.MainActivity">


</FrameLayout>

WelcomeScreenFragment.java: WelcomeScreenFragment.java:

package layout;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.alexoladele.testingshit.MainScreenFragment;
import com.alexoladele.testingshit.R;

// Declares WelcomeScreenFragment as subclass of fragment
public class WelcomeScreenFragment extends Fragment {
    private static final String TAG = "WelcomeScreenFragment";
    private Button startBtn;


    //    +++++++++++++++++++++ OVERRIDE - METHODS ++++++++++++++++++++++++++


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    //    Attaches the Fragment
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    // Creates Fragment view for use
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_welcome_screen, container, false);
        return view;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        startBtn = (Button) view.findViewById(R.id.start_screen_btn);
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                FragmentTransaction transaction = fm.beginTransaction();

                transaction
                        .replace(R.id.root_layout, MainScreenFragment.newInstance())
                        .addToBackStack(null)
                        .commit();
            }
        });
    }

    //    +++++++++++++++++++++ User METHODS ++++++++++++++++++++++++++

    // Method to create new instances of Fragment
    public static WelcomeScreenFragment newInstance() {
        return new WelcomeScreenFragment();
    }
}

fragment_welcome_screen.xml: fragment_welcome_screen.xml:

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/welcome_screen_root"
    tools:context="layout.WelcomeScreenFragment">

    <TextView
        android:id="@+id/start_screen_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="176dp"
        android:fontFamily="monospace"
        android:text="@string/welcome_message"
        android:textAlignment="center"
        android:textSize="13sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1"
        app:layout_constraintHorizontal_bias="0.6" />

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/start_screen_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/welcome_button"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="59dp"
        app:layout_constraintTop_toBottomOf="@+id/start_screen_msg"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        android:layout_below="@+id/start_screen_msg"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

MainScreenFragment.java: MainScreenFragment.java:

package com.alexoladele.testingshit;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainScreenFragment extends Fragment {
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

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

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public static MainScreenFragment newInstance() {
       return new MainScreenFragment();
    }
}

fragment_main_screen.xml: fragment_main_screen.xml:

<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/main_screen_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.alexoladele.testingshit.MainScreenFragment">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="163dp"
        android:text="@string/main_screen_tempmsg"
        android:textAlignment="center"
        android:visibility="visible"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintTop_creator="1" />


</RelativeLayout>

you did not inflate your fragment_main_screen xml ! 您没有为fragment_main_screen xml充气!

In your MainScreenFragment.java do that 在您的MainScreenFragment.java执行该操作

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

    }

while creating fragment, we need to attach fragment xml, same as we are attaching in activity, but the method is different. 在创建片段时,我们需要附加片段xml(与在活动中附加的片段相同),但是方法不同。

Add below code in MainScreenFragment.java: 在MainScreenFragment.java中添加以下代码:

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.fragment_main_screen, container, false);
// do your code here. 
    return view;

}

膨胀MainScreenFragment的布局也就像您为WelcomeScreenFragment所做的那样。

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

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