错误链接图像按钮导致片段错误:找不到适合的构造函数

问题描述 投票:0回答:1

大家好,我想让我的imagebutton在我的片段中工作。我需要更改什么?错误:找不到合适的构造函数

public class HomeFragment extends Fragment {
    Button myproductButton;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.home_fragment, container, false);

        myproductButton = myproductButton.findViewById(R.id.imageButton2);
        myproductButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HomeFragment.this, myProducts.class);
                startActivity(intent);
            }
        });

    }
}
android android-studio constructor fragment imagebutton
1个回答
0
投票

您应该先扩大您的布局,然后找到您的按钮

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.home_fragment, container, false);// inflate layout first

    myproductButton = view.findViewById(R.id.imageButton2);// here is how to get button
    myproductButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), myProducts.class); // you should pass activity as context
            startActivity(intent);
        }
    });
 return view;

}
© www.soinside.com 2019 - 2024. All rights reserved.