Java 片段在Android中不出现

Java 片段在Android中不出现,java,android,android-fragments,Java,Android,Android Fragments,我最近了解了Android中的片段,并正在构建一个记事本应用程序来练习它们 创意:应用背后的创意很简单。我按下按钮添加一张新便条。CardView视图组将变为可见。碎片将被放置在此CardView中 问题:当我按下按钮添加新音符时,片段不会弹出 MainActivity.java import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import androidx.car

我最近了解了Android中的片段,并正在构建一个记事本应用程序来练习它们

创意:应用背后的创意很简单。我按下按钮添加一张新便条。CardView视图组将变为可见。碎片将被放置在此CardView中

问题:当我按下按钮添加新音符时,片段不会弹出

MainActivity.java

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Objects;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private CardView fragmentCardView;
    private Button newNoteButton;
    private FragmentManager fragmentManager = getSupportFragmentManager();
    private FragmentTransaction fragmentTransaction;

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

        newNoteButton = findViewById(R.id.new_note_button);
        fragmentCardView = findViewById(R.id.fragment_cardView);
    }

    void setActionBar(){
        Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setCustomView(R.layout.layout_custom_action_bar);
    }

    void showCreateNoteFragment(){
        CreateNoteFragment createNoteFragment = CreateNoteFragment.newInstance();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_cardView, createNoteFragment);
        fragmentTransaction.commit();
        fragmentCardView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        if (v == newNoteButton)
            showCreateNoteFragment();
    }
}
package com.example.notepadapplication;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class CreateNoteFragment extends Fragment implements View.OnClickListener {

    private EditText inputEditText;

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

    static CreateNoteFragment newInstance(){
        return new CreateNoteFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_create_note, container, false);
        inputEditText = rootView.findViewById(R.id.input_editText);
        Button saveButton = rootView.findViewById(R.id.save_button);
        saveButton.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onClick(View v) {
        String saveButtonText = inputEditText.getText().toString();
    }
}
活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:background="#F2F2F2"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/notes_listView"
        android:layout_width="match_parent"
        android:layout_height="667dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <Button
        android:id="@+id/new_note_button"
        android:layout_width="350dp"
        android:layout_height="50dp"
        android:layout_marginTop="27dp"
        android:fontFamily="@font/nunito_bold"
        android:text="@string/new_note_button_string"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/notes_listView"/>

    <androidx.cardview.widget.CardView
        android:id="@+id/fragment_cardView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        android:layout_marginBottom="100dp"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:visibility="invisible"/>

</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginTop="100dp"
    android:layout_marginBottom="100dp"
    android:layout_marginStart="25dp"
    android:layout_marginEnd="25dp"
    android:background="#F2F2F2"
    tools:context=".CreateNoteFragment">

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

        <TextView
            android:id="@+id/header_textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="@string/display_fragment_header_string"
            android:layout_marginTop="10dp"
            android:textAlignment="center"
            android:textSize="20sp"
            android:fontFamily="@font/nunito_bold"/>

        <View
            android:id="@+id/divider_view"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="5dp"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:layout_below="@id/header_textView"
            android:background="#000000" />

        <EditText
            android:id="@+id/input_editText"
            android:layout_width="match_parent"
            android:layout_height="475dp"
            android:layout_below="@id/divider_view"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="20dp"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            android:padding="10dp"
            android:inputType="textMultiLine"
            android:gravity="top"
            android:background="#FFFFFF"
            tools:ignore="Autofill,LabelFor,TextFields" />

        <Button
            android:id="@+id/save_button"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:text="@string/save_button_string"
            android:fontFamily="@font/nunito_bold"/>

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
fragment\u create\u note.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:background="#F2F2F2"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/notes_listView"
        android:layout_width="match_parent"
        android:layout_height="667dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <Button
        android:id="@+id/new_note_button"
        android:layout_width="350dp"
        android:layout_height="50dp"
        android:layout_marginTop="27dp"
        android:fontFamily="@font/nunito_bold"
        android:text="@string/new_note_button_string"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/notes_listView"/>

    <androidx.cardview.widget.CardView
        android:id="@+id/fragment_cardView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        android:layout_marginBottom="100dp"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:visibility="invisible"/>

</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginTop="100dp"
    android:layout_marginBottom="100dp"
    android:layout_marginStart="25dp"
    android:layout_marginEnd="25dp"
    android:background="#F2F2F2"
    tools:context=".CreateNoteFragment">

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

        <TextView
            android:id="@+id/header_textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="@string/display_fragment_header_string"
            android:layout_marginTop="10dp"
            android:textAlignment="center"
            android:textSize="20sp"
            android:fontFamily="@font/nunito_bold"/>

        <View
            android:id="@+id/divider_view"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="5dp"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:layout_below="@id/header_textView"
            android:background="#000000" />

        <EditText
            android:id="@+id/input_editText"
            android:layout_width="match_parent"
            android:layout_height="475dp"
            android:layout_below="@id/divider_view"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="20dp"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            android:padding="10dp"
            android:inputType="textMultiLine"
            android:gravity="top"
            android:background="#FFFFFF"
            tools:ignore="Autofill,LabelFor,TextFields" />

        <Button
            android:id="@+id/save_button"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:text="@string/save_button_string"
            android:fontFamily="@font/nunito_bold"/>

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

有人能检查一下为什么碎片没有出现吗?谢谢你们的帮助,伙计们

注:我认为由于我使用的所有视图组,应用程序可能会变得有点臃肿。如果你们有更好的方法来设计这个,无论如何,我会喜欢任何输入

当我按下按钮添加新音符时,片段不会弹出

这是意料之中的,因为按下按钮时,您不知道该做什么

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

        newNoteButton = findViewById(R.id.new_note_button);
        // Button will call activity's onClick whenever it's clicked
        newNoteButton.setOnClickListener(this);
        fragmentCardView = findViewById(R.id.fragment_cardView);
    }
当我按下按钮添加新音符时,片段不会弹出

这是意料之中的,因为按下按钮时,您不知道该做什么

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

        newNoteButton = findViewById(R.id.new_note_button);
        // Button will call activity's onClick whenever it's clicked
        newNoteButton.setOnClickListener(this);
        fragmentCardView = findViewById(R.id.fragment_cardView);
    }

真不敢相信我犯了那个错误。我甚至没有注意到。谢谢。真不敢相信我犯了那个错误。我甚至没有注意到。非常感谢。