android 来自自定义对话框的片段调用

android fragment call from custom dialog

我正在从自定义对话框中调用片段。但我不能调用片段。 我的 onClick 调用函数

  public void text_noteClick(View v){
    Fragment fragments = new Text_Note_Fragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.layout.note_text, fragments);
    transaction.addToBackStack(null);
    Toast.makeText(getApplicationContext(),"text",Toast.LENGTH_SHORT).show();
}

Toast 运行成功。 Text_Note_Fragment class 是

public class Text_Note_Fragment extends Fragment {
public Text_Note_Fragment() {

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

} 我认为问题是片段替换功能。 对不起我的英语:) TNX

我猜这条线是根本原因。

transaction.replace(R.layout.note_text, fragments);

我看到您在 onCreateView 函数中使用了 R.layout.note_text,这就是您片段的布局 xml。但为什么你在替换功能中使用它?你应该像这样使用容器(通常是 FrameLayout)。

transaction.replace(R.id.container, fragments);