0

I cannot reproduce this bug, but it is live on production and firebase is telling me that this is a common crash. I am not sure how to reproduce this bug because everytime I go into the fragment it works fine on my end. I am not sure how to resolve this since I am a novice to Android.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.widget.Spinner.getBackground()' on a null object reference

Screenshot of Firebase StackTrace here

Positions Fragment (Took some unrelated code out due to security concerns)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        spinner = getToolbarSpinner().getBackground();
    } 
        @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View view = inflater.inflate(R.layout.fragment_positions, container, false);
}

___________________
View.java

    /**
     * Gets the background drawable
     *
     * @return The drawable used as the background for this view, if any.
     *
     * @see #setBackground(Drawable)
     *
     * @attr ref android.R.styleable#View_background
     */
    @InspectableProperty
    public Drawable getBackground() {
        return mBackground;
    }


Android Dev
  • 305
  • 3
  • 18
  • 1
    well this method -> `getToolbarSpinner()` is the issue and you have omitted its implementation. Just by looking at it though any `View` interactions before `onViewCreated` hook is called will probably have errors and inconsistent behaviour. If this is calling an `Activity` there is every chance it will throw a NPE - any view interaction inside a fragment should be done AFTER `onViewCreated` and any callbacks to the `Activity` from a `Fragment` should be done between `onAttach` and `onDetach` - preferably with a interface contract. – Mark Oct 20 '21 at 18:19
  • @MarkKeen Would it be wrong to call that method in onStart()?https://stackoverflow.com/questions/28929637/difference-and-uses-of-oncreate-oncreateview-and-onactivitycreated-in-fra/44582434#44582434 Based on this activity lifecycle chart, it seems like what would be the right answer would be onStart() – Android Dev Oct 20 '21 at 19:05
  • 1
    Based on the link that would seem reasonable .. IF you are calling an `Activity` view element from your `Fragment`, again it's not stated in your code. I suggest that it mght be better to tell the activity what to set, rather than trying to get and retain a variable - that could also cause memory leaks. – Mark Oct 20 '21 at 19:31

0 Answers0