簡體   English   中英

沒有找到 id 片段的視圖

[英]No view found for id fragment

當我按下 InputFragmentNew 片段中的按鈕時,我希望 newRegistrationFragment 打開。 但是當我嘗試打開它時,我遇到了以下問題。 如何從一個片段切換到另一個片段?

頭等艙

  public class GirisFragmentNew extends Fragment implements View.OnClickListener{

 btnKAyit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, new yeniKayıtFragment());
                fragmentTransaction.commit();
            }
        });
}

第二個片段

public class yeniKayıtFragment extends  Fragment {
    private yeniKayıtModel yeniKayıtModel;
   public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        yeniKayıtModel =
                ViewModelProviders.of(this).get(yeniKayıtModel.class);

        View root = inflater.inflate(R.layout.fragment_yenikayit, container, false);
        Window window=getActivity().getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.setNavigationBarColor(getResources().getColor(R.color.colorPrimary));}
  return root;
    }

二級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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="...yeniKayıtFragment">


    <FrameLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/fragment_container"
        />

    <WebView
        android:id="@+id/webviewYeniKayıt"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</FrameLayout>

在你的MainActivity創建這個公共變量public static FragmentManager manager;

然后在onCreate方法中manager = getSupportFragmentManager();

然后在你的GirisFragmentNew片段中

btnKAyit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    yeniKayıtFragment fragment = new yeniKayıtFragment();
                    MainActivity.manager.beginTransaction().replace(R.id.fragment_container,fragment,yenTAG).commit();
                }
            });

Fragment情況下,您必須使用getChildFragmentManager而不是getFragmentManager 不要使用您嘗試過的靜態變量。 檢查以下:

btnKAyit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, new yeniKayıtFragment());
        fragmentTransaction.commit();
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM