android ViewRootImpl.setPausedForTransition(boolean)当转换到过早调用的其他Activity时,ActivityTransitionCoordinator中出现NullPointerException

xe55xuns  于 2023-09-28  发布在  Android
关注(0)|答案(2)|浏览(101)

在我的Android应用程序中,我有一个启动屏幕,我在那里做一些设置和加载。我的应用程序使用默认的explode作为windowEnterTransitionwindowExitTransition以及changeImageTransform加上changeBounds转换集作为windowSharedElementEnterTransitionwindowSharedElementExitTransition。为了方便起见,我使用静态方法启动下一个Activity,在该方法中,我将当前Activity作为Context和共享元素传递。代码在这篇文章的第二部分提供。
其中一种情况是没有什么可加载的,所以app几乎立即触发下一个Activity。问题是,在这种情况下,应用程序莫名其妙地在ActivityTransitionCoordinator中崩溃,堆栈将在本文的下一部分给出。内部调试表明,ViewRootImpl是实现有null和没有null检查,所以调用viewRoot.setPausedForTransition(false)抛出一个NullPointerException。您可以在下面的代码中找到标记的这个不吉利的点。
为了关注这个失败的场景,让我们假设一个逻辑,决定没有什么要加载,下一个Activity应该立即启动,这是非常简单的,它可以简化为只启动所提到的活动。
如果在onCreate()onResume()onEnterAnimationComplete()方法中调用第二个Activity的开始,则没有区别。我甚至尝试在通过调用getWindow().getSharedElementEnterTransition()getWindow().getEnterTransition()获得的Transition上添加一个侦听器,以允许在Transition完成时启动下一个Activity。给定的Transitions不为null,但应用程序从不进入附加侦听器的方法。
我现在使用的解决方法是调度一个Runnable来延迟调用下一个Activity
我想知道这是否是一个Android(更具体地说是一个SupportLibrary)问题,或者我错过了什么。有人遇到过类似的问题吗?
一个堆栈跟踪:

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)

ActivityTransitionAnimator中的代码失败:

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();
    }
}

启动下一个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内容(简化):

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, "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");
                    }
                });
            }
        }
    }

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

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

lvmkulzt1#

我也面临着同样的问题,而工作与爆炸活动过渡,我发现一件事,这段代码正常运行在棒棒糖版本,但崩溃以上棒棒糖。连我都找不出崩溃的原因。但我用另一种方式解决它。只需在活动转换中添加延迟。我在下面给出了代码

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());
}}

添加此功能后,在所有版本中正常工作。

x33g5p2x

x33g5p2x2#

您可以使用根视图post方法导航到下一个活动,而不是使用Handler

findViewById(<Your activity root view id>).post(() -> navigate());

通常,根视图可以是android.R.id.content

相关问题