簡體   English   中英

無法使用導航情節提要板在片段中添加按鈕以從一個片段跳轉到另一個片段

[英]Unable to add a button in fragment using Navigation storyboard to jump from one fragment to another fragment

我試圖在片段頁面中添加一個按鈕,以使用android studio導航情節提要的方法從一個片段跳轉到另一個片段。 但是,在Mainfragment.java中,我的按鈕ID無法解析,無法識別我的按鈕ID,也不知道為什么。 請幫忙!

  public MainFragment() {

        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);


    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Button goToBtn = (Button) this.findViewById(R.id.goToBtn);
        goToBtn.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.toAccount, null));


    }
    }

對於Mainfragment.java的上述代碼, findViewById為紅色。

在行下更改

Button goToBtn = (Button) this.findViewById(R.id.goToBtn);

Button goToBtn = (Button) view.findViewById(R.id.goToBtn);

暫無
暫無

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

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