Если корзина пуста , или при повторном заполнеии корзины выводится множество ошибок…
- User
Если корзина пуста , или при повторном заполнеии корзины выводится множество ошибок исправь это : package com.example.myapp_2.Data.cart;
import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;
import com.example.myapp_2.R;
import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;
import com.example.myapp_2.R;
public class CartFragment extends Fragment implements Cart.OnCartChangedListener {
code - Assistant
The provided code seems to be correct. If the cart is empty, it will simply display an empty cart screen. If there are errors during the process of filling the cart, please provide more information so that we can help you better.
- User
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myapp_2, PID: 9711 java.lang.IllegalStateException: Fragment CartFragment{aad850f} (3ac90f54-00e6-4926-bf6f-cd769ca60676) not attached to a context. at androidx.fragment.app.Fragment.requireContext(Fragment.java:900) at androidx.fragment.app.Fragment.getResources(Fragment.java:964) at androidx.fragment.app.Fragment.getString(Fragment.java:999) at com.example.myapp_2.Data.cart.CartFragment.updateTotalPrice(CartFragment.java:94) at com.example.myapp_2.Data.cart.CartFragment.onCartChanged(CartFragment.java:100) at com.example.myapp_2.Data.cart.Cart.notifyCartChanged(Cart.java:77) at com.example.myapp_2.Data.cart.Cart.clear(Cart.java:64) at com.example.myapp_2.Data.cart.CartFragment3600(View.java:810) at android.view.ViewMethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
- Assistant
The error occurs because the fragment is not attached to a context when
updateTotalPrice()method is called inCartFragment. UserequireContext().getString()instead ofgetString()inside theupdateTotalPrice()method ofCartFragmentas shown below:codeprivate void updateTotalPrice() { double totalPrice = cart.getTotalPrice(); totalPriceTextView.setText(requireContext().getString(R.string.cart_item_total_price, totalPrice)); }This should resolve the error.
- User
Ошибки : Process: com.example.myapp_2, PID: 9990 java.lang.IllegalStateException: Fragment CartFragment{4b191dd} (e8cd45ca-9a71-4dea-aa46-d993013c955a) not attached to a context. at androidx.fragment.app.Fragment.requireContext(Fragment.java:900) at com.example.myapp_2.Data.cart.CartFragment.updateTotalPrice(CartFragment.java:94) at com.example.myapp_2.Data.cart.CartFragment.onCartChanged(CartFragment.java:99) at com.example.myapp_2.Data.cart.Cart.notifyCartChanged(Cart.java:77) at com.example.myapp_2.Data.cart.Cart.clear(Cart.java:64) at com.example.myapp_2.Data.cart.CartFragment$2.onClick(CartFragment.java:79) at android.view.View.performClick(View.java:7448)
- Assistant
This error message is similar to the previous one and probably caused by the same reason.
Change the
getString()method torequireContext().getString()in theupdateTotalPrice()method ofCartFragmentas shown below:codeprivate void updateTotalPrice() { double totalPrice = cart.getTotalPrice(); totalPriceTextView.setText(requireContext().getString(R.string.cart_item_total_price, totalPrice)); }Using
requireContext()instead ofgetContext()orgetActivity()ensures that the fragment is attached to a context before retrieving the resources. - User
исправил , но шибка не исчезла
- Assistant
In that case, try to use
getApplicationContext()instead ofrequireContext()in theupdateTotalPrice()method ofCartFragment.codeprivate void updateTotalPrice() { double totalPrice = cart.getTotalPrice(); totalPriceTextView.setText(getApplicationContext().getString(R.string.cart_item_total_price, totalPrice)); }This should ensure that the application context is used instead of the fragment's context.