我正在嘗試使用android studio的導航故事板的方法在片段頁面中添加一個按鈕以從一個片段跳轉(zhuǎn)到另一個片段。但是,在 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是紅色的。
1 回答

溫溫醬
TA貢獻1752條經(jīng)驗 獲得超4個贊
改變線下
Button goToBtn = (Button) this.findViewById(R.id.goToBtn);
和
Button goToBtn = (Button) view.findViewById(R.id.goToBtn);
添加回答
舉報
0/150
提交
取消