ошибка ссылка на изображение кнопка для фрагментирования ошибка: не найден подходящий конструктор для - PullRequest
0 голосов
/ 26 марта 2020

Привет, ребята, так что я пытаюсь заставить мою кнопку изображения работать с моим фрагментом. Что мне нужно изменить? ошибка: не найден подходящий конструктор

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);
            }
        });

    }
}

1 Ответ

0 голосов
/ 26 марта 2020

сначала вы должны раздуть свой макет, а затем найти кнопку

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;

}
...