A
A
AndreyRafalsky132019-02-26 09:29:06
Android
AndreyRafalsky13, 2019-02-26 09:29:06

Trigger animation of the fragment only when the fragment is visible (PagerView). Why am I getting a NullPointerException?

Good afternoon. Please tell me how to solve the problem. There is a fragment that needs to start animating when it becomes visible. Scrolling elements is implemented using PagerView. Accordingly, not only the fragment that is visible at the given moment is created, but also neighboring fragments. And if animation starts for a visible fragment, then it starts for neighboring fragments too. Therefore, when I flip to the next fragment, the animation on it has already ended.
In the fragment, everything is implemented in the following way.
In the onViewCreated() method I call 3 methods:
1) init() (initialization)

private void init(View view) {
            detailImage = view.findViewById(R.id.detail_image);
            detailCreator = view.findViewById(R.id.creator_detail);
            detailLikes = view.findViewById(R.id.likes_detail);
            detailDownloads = view.findViewById(R.id.downloads_detail);
            detailFavorites = view.findViewById(R.id.favourites_detail);
    
            layout = view.findViewById(R.id.root_detail_layout);
            animationDrawable = (AnimationDrawable) layout.getBackground();
    
            animationDrawable.setEnterFadeDuration(5000);
            animationDrawable.setExitFadeDuration(2000);
        }

2) setData() (filling with data)
private void setData() {
        PhotoItem item = (PhotoItem) getArguments().getSerializable(ITEM_ARG);
        Picasso.get().load(item.getImageUrl()).fit().centerInside().into(detailImage);
        detailCreator.setText(item.getCreatorName());
        detailLikes.setText(String.format(getString(R.string.likes_info), item.getLikes()));
        detailDownloads.setText(String.format(getString(R.string.downloads_info), item.getDownloads()));
        detailFavorites.setText(String.format(getString(R.string.favorites_info), item.getFavorites()));
    }

3) startAnimations()
public void startAnimations(float translationY, int duration) {
            detailImage.animate().translationY(translationY).setDuration(duration).start();
            detailCreator.animate().translationY(translationY).setDuration(duration).setStartDelay(duration).start();
            detailLikes.animate().translationY(translationY).setDuration(duration).setStartDelay(2*duration).start();
            detailDownloads.animate().translationY(translationY).setDuration(duration).setStartDelay(3*duration).start();
            detailFavorites.animate().translationY(translationY).setDuration(duration).setStartDelay(4*duration).start();
    }

I'm trying to implement animation start for only one fragment using ViewPager.OnPageChangeListener:
@Override
    public void onPageSelected(int i) {
        PhotoDetailFragment fragment = 
    (PhotoDetailFragment)adapter.instantiateItem(pager, i);
        fragment.startAnimations(50f, 1000);
    }

I get a NullPointerException, and not every time, which is very strange
> E/AndroidRuntime: FATAL EXCEPTION: main
> Process: com.rafalsky.lesson1, PID: 14944
> java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rafalsky.lesson1/com.rafalsky.lesson1.view.activity. PhotoDetailActivity}:
> java.lang.NullPointerException: Attempt to invoke virtual method
> 'android.view.ViewPropertyAnimator android.widget.ImageView.animate()'
> on a null object reference
> at android.app.ActivityThread.performLaunchActivity(ActivityThread. java:2913)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
> at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
> at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
> at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
> at android.app.ActivityThread$H.handleMessage(ActivityThread. java:1808)
> at android.os.Handler.dispatchMessage(Handler.java:106)
> at android.os.Looper.loop(Looper.java:193)
> at android.app.ActivityThread.main(ActivityThread.java: 6669)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
> at com.android.internal.os.ZygoteInit .main(ZygoteInit.java:858)
> Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewPropertyAnimator
> android.widget.ImageView.animate()' on a null object reference
> at com.rafalsky.lesson1.view.fragment.PhotoDetailFragment .startAnimations(PhotoDetailFragment.java:50)
> at com.rafalsky.lesson1.view.activity.PhotoDetailActivity$2.onPageSelected(PhotoDetailActivity.java:70)
> at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java: 1947)
> at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:665)
> at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:631)
> at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:612)
> at com.rafalsky.lesson1.view.activity.PhotoDetailActivity.init(PhotoDetailActivity.java:79)
> at com.rafalsky.lesson1. view.activity.PhotoDetailActivity.onCreate(PhotoDetailActivity.java:33)
> at android.app.Activity.performCreate(Activity.java:7136) > at android.app.Activity.performCreate(Activity.java:7127) >
at android.app.Activity.performCreate(Activity.java:7127)
app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
> at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
> at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
> at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor. java:68)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
> at android.os.Handler.dispatchMessage(Handler.java:106)
> at android.os.Looper.loop(Looper. java:193)
> at android.app.ActivityThread.main(ActivityThread.java:6669)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Why is this happening, because I call the init() method in the onViewCreated() method. I went through the debugger, in the imageView there is a link, not null. Please tell me how to solve the problem. Thanks in advance for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Emelyanov, 2019-03-07
@MAGISTR_BRU

Good afternoon,
The NullPointerException error occurs because the startAnimations() function is called before the View is created.
The point at which the fragment becomes visible to the user inside the PagerView can be determined by overriding the setUserVisibleHint() function inside the fragment. More information can be found at https://stackoverflow.com/questions/10024739/how-t...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question