我正在嘗試使用android studio的導(dǎo)航故事板的方法在片段頁面中添加一個(gè)按鈕以從一個(gè)片段跳轉(zhuǎn)到另一個(gè)片段。但是,在 Mainfragment.java 中,我的按鈕 id 無法解析,它無法識(shí)別我的按鈕 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是紅色的。
1 回答
溫溫醬
TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊
改變線下
Button goToBtn = (Button) this.findViewById(R.id.goToBtn);
和
Button goToBtn = (Button) view.findViewById(R.id.goToBtn);
添加回答
舉報(bào)
0/150
提交
取消
