Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Calling a @Background/@WakeLock method in a @AfterInject method causes NullPointerException #1466

Description

Hi there, I'm experience a bug with AndroidAnnotations 3.3.1.

I have an @efragment support fragment with an @AfterInject method where I want to perform some initialization. Calling a @Background/@Wakelock annotated method from within the @AfterInject method results in a NullPointerException.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.PowerManager$WakeLock android.os.PowerManager.newWakeLock(int, java.lang.String)' on a null object reference : ... )

Looking at the generated code I see this:

private void init_(Bundle savedInstanceState) {
        OnViewChangedNotifier.registerOnViewChangedListener(this);
        Resources resources_ = getActivity().getResources();
        ...
        restoreSavedInstanceState_(savedInstanceState);
        init(); <- This is my @AfterInject method
        powerManager_ = ((PowerManager) getActivity().getSystemService(Context.POWER_SERVICE));
    }

The problem is that the powerManager_ is initialized after calling my @AfterInject method and thus the NullPointerException.

Shouldn't any required System Components/Services be initialized before calling any annotated method?

The fix for this would be to call any annotated methods after having first the reference to the required System Components/Services.

Putting my initialization code in a @Afterviews method doesn't result in the same exception.

Thanks in advance.

Activity

  1. yDelouis commented on Jun 11, 2015

    @yDelouis
    Contributor

    The injection of powerManager_ should be in init();, before calls to methods annotated with @AfterInject. This is indeed a bug, and it's caused by the fact that the annotation @WakeLock must be processed after other annotations and it's badly managed.

    @WonderCsabo, We should refactor a bit this part to make handlers more independent. In init_ we should create a block or a method for both inject, afterInject, injectViews, afterInjectViews, etc... So that the order of the handlers won't matters anymore.

  2. WonderCsabo commented on Jun 11, 2015

    @WonderCsabo
    Member

    @yDelouis i totally agree. I vote for a block.

  3. yDelouis commented on Jun 11, 2015

    @yDelouis
    Contributor

    I prefer a method so that it's clearer in the generated code.

  4. yDelouis commented on Nov 11, 2015

    @yDelouis
    Contributor

    Fixed by #1585.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions