Java – viewrootimpl. In viewtransitioncoordinition Setpausedfortransition (Boolean) when NullPointerException is converted to another activity called prematurely

In my Android application, I have a startup screen where I make some settings and loads My application uses the default explosion as the windowsentertransition, and the windowsexittransition and changeimagetransform plus the changeboundaries transformation are set to windowssharedelemententertransition and windowssharedelementexittransition For convenience, I use a static method to start the next activity, where I pass the current activity as a context and shared element The second part of this post provides this code

One scenario is that nothing can be loaded, so the app will trigger the next activity almost immediately The problem is that in this case, the application mysteriously crashes in the activity transition coordinator in some way, and a stack is given in the next part of this post The debug interior shows that the viewrootimpl implemented there is null and there is no null check, so viewroot. Exe is called Setpausedfortransition (false) will throw NullPointerException You can find this unlucky place in the following code

In order to focus on this failure scenario, let's assume that a logic decides that there is nothing to load, and the next activity should be started immediately. This is very simple and can be simplified to the activity mentioned at the beginning

If you call the beginning of the second Activity in onCreate (), onResume () or onEnterAnimationComplete (), there is no difference. I even tried to add a listener to the transition obtained by calling GetWindow () Getsharedelemententertransition() and getwindow() Getentertransition () allows you to start the next activity when the transformation is complete The given transitions are not null, but app will never enter the method of attaching listeners

The solution I'm using now is to schedule runnable to call the next activity delay

I wonder if this is an Android (a more specific support library) problem or what I missed Who has encountered similar problems?

Stack trace:

08-12 00:35:32.550 26453-26453/com.faver.mkoslacz.faverdemo E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.faver.mkoslacz.faverdemo,PID: 26453
                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewRootImpl.setPausedForTransition(boolean)' on a null object reference
                                                                                  at android.app.ActivityTransitionCoordinator.startInputWhenTransitionsComplete(ActivityTransitionCoordinator.java:897)
                                                                                  at android.app.ActivityTransitionCoordinator.viewsTransitionComplete(ActivityTransitionCoordinator.java:885)
                                                                                  at android.app.ExitTransitionCoordinator.getExitTransition(ExitTransitionCoordinator.java:318)
                                                                                  at android.app.ExitTransitionCoordinator.beginTransitions(ExitTransitionCoordinator.java:365)
                                                                                  at android.app.ExitTransitionCoordinator.-wrap0(ExitTransitionCoordinator.java)
                                                                                  at android.app.ExitTransitionCoordinator$4.run(ExitTransitionCoordinator.java:216)
                                                                                  at android.app.ActivityTransitionCoordinator.startTransition(ActivityTransitionCoordinator.java:773)
                                                                                  at android.app.ExitTransitionCoordinator.startExit(ExitTransitionCoordinator.java:213)
                                                                                  at android.app.ActivityTransitionState.startExitOutTransition(ActivityTransitionState.java:317)
                                                                                  at android.app.Activity.cancelInputsAndStartExitTransition(Activity.java:3960)
                                                                                  at android.app.Activity.startActivityForResult(Activity.java:3936)
                                                                                  at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
                                                                                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
                                                                                  at android.app.Activity.startActivity(Activity.java:4196)
                                                                                  at com.faver.mkoslacz.faverdemo.activity.AuthorizationActivity.startWithTransiton(AuthorizationActivity.java:45)
                                                                                  at com.faver.mkoslacz.faverdemo.activity.SplashActivity.onEnterAnimationComplete(SplashActivity.java:27)
                                                                                  at android.app.Activity.dispatchEnterAnimationComplete(Activity.java:5852)
                                                                                  at android.app.ActivityThread.handleEnterAnimationComplete(ActivityThread.java:2668)
                                                                                  at android.app.ActivityThread.-wrap10(ActivityThread.java)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1558)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Code in activitytransitionanimator failed:

private void startInputWhenTransitionsComplete() {
    if (mViewsTransitionComplete && mSharedElementTransitionComplete) {
        final View decor = getDecor();
        if (decor != null) {
            final ViewRootImpl viewRoot = decor.getViewRootImpl(); // it's null
            viewRoot.setPausedForTransition(false); // crashes here
        }
        onTransitionsComplete();
    }
}

Method to start the next activity:

public static void startWithTransiton(Activity activity,android.view.View logo) {
    Intent intent = new Intent(activity,AuthorizationActivity.class);
    ActivityOptionsCompat options = ActivityOptionsCompat
            .makeSceneTransitionAnimation(
                    activity,logo,activity.getString(R.string.logoTransfer));
    activity.startActivity(intent,options.toBundle());
}

Splash activity content (Simplified):

public class SplashActivity extends AppCompatActivity {

    private static final String TAG = "SplashActivity";

    private View logo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        logo = findViewById(R.id.logo);

//        AuthorizationActivity.startWithTransiton(this,logo); // will fail

        new Handler().postDelayed(() -> {
            AuthorizationActivity.startWithTransiton(this,logo); // executes flawlessly
        },300);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            Transition sharedElementEnterTransition = getWindow().getSharedElementEnterTransition();
            if (sharedElementEnterTransition != null) {
                sharedElementEnterTransition.addListener(new Transition.TransitionListener() {
                    @Override
                    public void onTransitionStart(Transition transition) {
                        Log.d(TAG,"onTransitionStart: never executes");
                    }

                    @Override
                    public void ontransitionend(Transition transition) {
                        Log.d(TAG,"ontransitionend: never executes");
                    }

                    @Override
                    public void onTransitionCancel(Transition transition) {
                        Log.d(TAG,"onTransitionCancel: never executes");
                    }

                    @Override
                    public void onTransitionPause(Transition transition) {
                        Log.d(TAG,"onTransitionPause: never executes");
                    }

                    @Override
                    public void onTransitionResume(Transition transition) {
                        Log.d(TAG,"onTransitionResume: never executes");
                    }
                });
            }

            Transition enterTransition = getWindow().getEnterTransition();
            if (enterTransition != null) {
                enterTransition.addListener(new Transition.TransitionListener() {
                    @Override
                    public void onTransitionStart(Transition transition) {
                        Log.d(TAG,"onTransitionResume: never executes");
                    }
                });
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        AuthorizationActivity.startWithTransiton(this,logo); // will fail
    }

    @Override
    public void onEnterAnimationComplete() {
        super.onEnterAnimationComplete();
//        AuthorizationActivity.startWithTransiton(this,logo); // will fail
    }
}

Solution

I also faced the same problem when using explosive activity conversion. I found that this code worked normally in lolipop version, but crashed on lolipop

public class SplashActivity extends AppCompatActivity {

final Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            navigate();
        }
    },500);
}

private void navigate() {
    ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this);
    Intent intent = new Intent(SplashActivity.this,MainActivity.class);
    startActivity(intent,options.toBundle());
}}

After adding this function normally in all versions

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>