Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException when using tooltip 2 times #28

Closed
khrizt opened this issue Feb 10, 2016 · 7 comments
Closed

NullPointerException when using tooltip 2 times #28

khrizt opened this issue Feb 10, 2016 · 7 comments

Comments

@khrizt
Copy link

khrizt commented Feb 10, 2016

Hi, I'm trying to use the tooltip library on 2 different activities of my project and when the 2nd should be shown this exception appears:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:525)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
    at android.view.View.layout(View.java:15654)
    at android.view.ViewGroup.layout(ViewGroup.java:4969)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2102)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1859)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1078)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
    at android.view.Choreographer.doFrame(Choreographer.java:550)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5376)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

The code I'm using is

Tooltip.make(itemView.getContext(),
                     new Tooltip.Builder()
                             .withStyleId(R.style.ToolTipLayoutCustomStyle)
                             .anchor(itemView.findViewById(R.id.detail_choice_optionA), Tooltip.Gravity.BOTTOM)
                             .closePolicy(new Tooltip.ClosePolicy()
                                     .insidePolicy(true, false)
                                     .outsidePolicy(true, false), 5000)
                             .activateDelay(1500)
                             .showDelay(300)
                             .text(itemView.getResources().getString(R.string.tooltip_choice_text))
                             .maxWidth(400)
                             .withArrow(true)
                             .withOverlay(true)
                             .floatingAnimation(Tooltip.AnimationBuilder.DEFAULT)
                             .build()
                ).show();

Any hint what's happening? Thanks!

@khrizt
Copy link
Author

khrizt commented Feb 16, 2016

Looks like the problem is that using an adapter the cells can be redrawn whenever it chooses, so that was the origin of the problem

@khrizt khrizt closed this as completed Feb 16, 2016
@michaeltnguyen
Copy link

Were you able to fix the issue? Or is the Tooltip not usable within an adapter?

@eduardbosch
Copy link

I'm having this same issue in some devices.

Is there any fix on this?

@strmchsr
Copy link

having this issue on Nexus 5

@eduardbosch
Copy link

I haven't took a look at the code, but is this possible to fix to us within an adapter?

@Sdghasemi
Copy link

The NPE is because of RecyclerView recreating views due to notifyDataSetChanged() or any other method which removes old views from ViewHolder and replace them with new ones, and the anchor view is one of them. So i moved my view declaration (just anchor) to the adapter so the reference remains for my tooltip after refilling views and it worked. Here is my code:

private ProgressBar progressBar;
private class HeaderViewHolder extends RecyclerView.ViewHolder {
    private ImageView avatarImageView;
    private Tooltip.TooltipView tooltip;

    public HeaderViewHolder(View view) {
        progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
        avatarImageView = (ImageView) view.findViewById(R.id.avatar_image_view);

        tooltip = Tooltip.make(mContext,
                new Tooltip.Builder()
                        .anchor(progressBar, Tooltip.Gravity.BOTTOM)
                        .closePolicy(new Tooltip.ClosePolicy()
                                .insidePolicy(false, true)
                                .outsidePolicy(true, true), 0)
                        .activateDelay(0)
                        .withCustomView(R.layout.profile_info_tooltip)
                        .withStyleId(R.style.TooltipStyle)
                        .withArrow(true)
                        .withOverlay(false)
                        .build()
        );
    }

    public void bindView(final Profile profile) {
        if (profile == null) return;
        .
        .
        .
        avatarImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tooltip.show();
            }
        });
    }
}

Hope it helps somebody.

@eduardbosch
Copy link

These PRs solves this problem:
#93
#71

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants