大家好,我想让我的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);
}
});
}
}
您应该先扩大您的布局,然后找到您的按钮
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;
}