无法使用导航情节提要板在片段中添加按钮以从一个片段跳转到另一个片段

时间:2018-08-05 10:46:09

标签: java android navigation storyboard

我正尝试在片段页面中添加一个按钮,以使用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为红色。

1 个答案:

答案 0 :(得分:0)

在行下更改

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

使用

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