java - fragment 不会在 Activity 中显示

标签 java android android-fragments

我对 Android 开发非常陌生。我浏览了几个有关 fragment 的教程,但由于某种原因,我似乎无法从主视图中添加 fragment 。我不想做任何痛苦而复杂的事情。我想要的只是创建我的 fragment 并将它们添加到我的主要 Activity 中。就是这样。但每次我运行我的应用程序时,我得到的只是一个空白屏幕。我调试它,它会进入 fragment 。这让我相信问题可能出在 xml laout 文件中(也许)。

这是我的 MainActivity.java

package com.android.myCompany.nameOfApp;

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

public class MainActivity extends AppCompatActivity {

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

        // Begin the transaction
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.foo_frame_layout, new FooFragment());
        ft.commit();
    }
}

这是我的 Fragment.cs

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 */
public class FooFragment extends Fragment {

    public FooFragment () {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());
        textView.setText(R.string.hello_blank_fragment);
        return textView;
    }
}

这是我的布局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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/background_portrait">

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

这是我的fragment_foo.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/copyright_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FooFragment">
<!-- TODO: Update blank fragment layout -->

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="102dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="8dp"
        android:adjustViewBounds="false"
        android:cropToPadding="false"
        app:layout_constraintBottom_toTopOf="@+id/imageView6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.504"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo" />

    <ImageView
</RelativeLayout>

谁能告诉我我的设置有什么问题吗?非常感谢。

最佳答案

在 fragment 类中对此进行更改:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_foo, container, false); // This line will inflate your fragment_foo layout and return it's rootview for fragment inflation
}

然后在fragment的onViewCreated方法中找到你的fragment View 。

关于java - fragment 不会在 Activity 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53348177/

相关文章:

java - 使用 Jaxb API 将 XML 解码为 Java 对象时获取 NullPointerException

java - 接收JSON java.lang.string无法转换为jsonarray

android - 为什么我应该在主要 Activity 和 fragment 之间进行通信时实现接口(interface)

android - 在 fragment 之间滑动时消失的操作栏按钮

java - 安卓。 fragment 不保存它们的状态

java - 具有相同基类的泛型

java - 在 Java 中分配随机套接字时为什么要等待 `isBound`?

android - Android 4.0.3+ 上的全局音频和 session ID

android - IntentService 和 HandlerThread 计数

android - 在DialogFragment中,onCreate应该做什么?