0

I'm newbie in programming. I'am adding entries to spinner with data from firebase, everything is ok, but when im fast switching between fragments, error occurs. I know the problem is that im using "getActivity" while using fragment, and the code is still excecuting, while the fragment is not attached anymore. And thats the problem. Is there any posibility to attach adapter to spinner from an activity instead of from fragment? Imo that could avoid that problem, but i dont know how to do that? Here is some code.


                ArrayAdapter dania = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, menulist);  //I KNOW THAT THE PROBLEM IS HERE getActivity() 

                ArrayAdapter adodatki = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, menudodatkilist); //I KNOW THAT THE PROBLEM IS HERE getActivity() 

                dania.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                adodatki.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                danie1spinner.setAdapter(dania);
                danie2spinner.setAdapter(adodatki);

I'm open for all suggestions. Thanks for all answers :)

Respin
  • 1
  • 2
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody Apr 04 '20 at 14:23
  • You need to put this code in a part of the fragment lifecycle where getActivity() is never null. – Steve M Apr 04 '20 at 14:34
  • It's very difficult to debug a crash without a stack trace. See [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788) for advice on what to do once you have the stack trace. If you still need help, edit your question to include the **complete stack trace**, as well as **which line of your code** the stack trace points to. – Ryan M Apr 06 '20 at 02:35

2 Answers2

0

Thanks @SteveM, For me it worked to use

if (getActivity() != null)
{
 ArrayAdapter dania = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, menulist);  //I KNOW THAT THE PROBLEM IS HERE getActivity() 

                ArrayAdapter adodatki = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, menudodatkilist); //I KNOW THAT THE PROBLEM IS HERE getActivity() 

                dania.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                adodatki.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                danie1spinner.setAdapter(dania);
                danie2spinner.setAdapter(adodatki);
}

thanks one more time for an easy idea :D

Respin
  • 1
  • 2
0

ArrayAdapter requires a Context as the first parameter. Simply pass the Context of the Fragment instead. (I think it's GetView())

Zun
  • 1,553
  • 3
  • 15
  • 26