0

I don't know why app crashes when reaching for id of others element. Do I need to add something more or give my phone permission?

    @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                        @Nullable Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View searchProduct = inflater.inflate(R.layout.fragment_search_category, container, false);
    Spinner productCategory = searchProduct.findViewById(R.id.spnCategory);
    category = productCategory.getSelectedItem().toString();

    productName = searchProduct.findViewById(R.id.edtSearch).toString();
    //getAPIDataProduct(category, productName);


    return searchProduct;
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Zoe Mar 12 '19 at 18:17
  • 1
    your problem can be more clear if you share your crash report – Beyazid Mar 12 '19 at 19:11

1 Answers1

0

If you don't have any items in Spinner, then the line category = productCategory.getSelectedItem().toString(); will crash for NullPointerException, because getSelectedItem() will return null as it says in the document. You need to add items in the Spinner first!

vilpe89
  • 4,656
  • 1
  • 29
  • 36