我正在嘗試在片段中創建一個 ImageView 按鈕以在導航抽屜中打開另一個片段,但我遇到了一個問題,我無法將 OnCliCkListener 設定為我的 ImageView 按鈕。我嘗試在 youtube 上搜索修復程式,但似乎找不到解決問題的方法。
這是錯誤 這是文本形式的錯誤
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>
uj5u.com熱心網友回復:
您正在膨脹FragmentHomeBinding,fragment_home.xml但帶有 imageview 的布局被稱為Dashboard_fragment.xml. 將 移動ImageView到fragment_home.xml可以這樣使用它。
uj5u.com熱心網友回復:
再次檢查代碼,在 onCreateView 和 onViewCreated 函式之前缺少 @Override
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/513344.html
上一篇:無法從一個片段觀察Viewmodel但適用于Android[Kotlin]中的不同片段
下一篇:com.chargeatfriends.android.ui.reserve.ReserveFragment:呼叫Fragment建構式導致例外?
