I'm trying to make an ImageView button inside a fragment to open another fragment in a navigation drawer but I have encountered a problem where I can't set an OnCliCkListener to my ImageView button. I tried searching fixes on youtube but I can't seem to find a fix to my problem.
Here's the error Here's the error as text
HomeFragment.java
public class HomeFragment extends Fragment {
private FragmentHomeBinding binding;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(getLayoutInflater());
return binding.getRoot();
}
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
binding.IVOrdersButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment OrdersFragment = new OrdersFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.nav_dashboard, OrdersFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
public void onDestroy(){
super.onDestroy();
binding = null;
}
}
Dashboard_fragment.xml
<TextView
android:id="@ id/DashboardTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Dashboard"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.051"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.022" />
<ImageView
android:id="@ id/IVOrdersButton"
android:layout_width="76dp"
android:layout_height="89dp"
android:clickable="true"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/DashboardTV"
app:layout_constraintVertical_bias="0.074"
app:srcCompat="@drawable/ic_orders"
tools:srcCompat="@drawable/ic_orders" />
<TextView
android:id="@ id/OrdersLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ordersDashboard"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.187"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/IVOrdersButton"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
CodePudding user response:
You're inflating FragmentHomeBinding
based on fragment_home.xml
but the layout with the imageview is called Dashboard_fragment.xml
. Move the ImageView
to fragment_home.xml
to be able to use it this way.
CodePudding user response:
Check the code again, it is missing @Override before the onCreateView and onViewCreated functions