android-fragments 为什么我的ImageView作为按钮不起作用?

vbopmzt1  于 2022-11-13  发布在  Android
关注(0)|答案(2)|浏览(182)

我尝试在一个片段中创建一个ImageView按钮,以便在导航抽屉中打开另一个片段,但我遇到了一个问题,无法将OnCliCkListener设置为ImageView按钮。我尝试在youtube上搜索修复程序,但似乎找不到解决问题的方法。
Here's the errorHere'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>

u7up0aaq

u7up0aaq1#

你在fragment_home.xml的基础上膨胀了FragmentHomeBinding,但是带有imageview的布局叫做Dashboard_fragment.xml。把ImageView移到fragment_home.xml,这样就可以使用它了。

eblbsuwk

eblbsuwk2#

再次检查代码,在onCreateView和onViewCreated函数之前缺少@Override

相关问题