gpt4 book ai didi

java - 为什么我的 fragment 是空白的?

转载 作者:行者123 更新时间:2023-12-01 20:56:26 29 4
gpt4 key购买 nike

大家好!我试图在编码时学习 fragment ,并且尝试将一个 fragment 与另一个 fragment 切换。每当我单击下一个按钮时,当前 fragment 就会被替换为空白 fragment ,我不知道为什么!

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:

<?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:

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:

<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:

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:

<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>

最佳答案

您没有膨胀您的 fragment_main_screen xml!

在您的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;

}

关于java - 为什么我的 fragment 是空白的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42291746/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com