java – ViewTransitionCoordinition中的ViewRootImpl.setPausedForTransition(boolean)NullPointerException转换到过早调用的其他Activity时

前端之家收集整理的这篇文章主要介绍了java – ViewTransitionCoordinition中的ViewRootImpl.setPausedForTransition(boolean)NullPointerException转换到过早调用的其他Activity时前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的 @L_301_0@应用程序中,我有一个启动画面,我在那里进行一些设置和加载.我的应用程序使用默认爆炸作为windowEnterTransition,windowExitTransition和changeImageTransform加上changeBounds转换设置为windowSharedElementEnterTransition和windowSharedElementExitTransition.为方便起见,我使用静态方法启动下一个Activity,其中我将当前Activity作为Context和共享元素传递.该帖子的第二部分提供了该代码.

其中一个场景是没有任何东西可以加载,因此app几乎会立即触发下一个Activity.问题是,在这种情况下,应用程序以某种方式神秘地在ActivityTransitionCoordinator中崩溃,并在此帖子的下一部分中给出了一个堆栈.调试内部显示在那里实现的ViewRootImpl为null并且没有空检查,因此调用viewRoot.setPausedForTransition(false)会抛出NullPointerException.你可以在下面的代码中找到这个不吉利的地方.

为了关注这个失败的场景,让我们假设一个逻辑决定没有任何东西要加载,而下一个Activity应该立即启动,这很简单,可以简化为刚开始提到的活动.

如果在onCreate(),onResume()或onEnterAnimationComplete()方法调用第二个Activity的开头,则没有区别.我甚至尝试在通过调用getWindow()获取的Transition上添加一个侦听器.getSharedElementEnterTransition()和getWindow().getEnterTransition()允许在转换完成时启动下一个Activity.给定的Transitions不为null,但app永远不会进入附加侦听器的方法.

我现在使用的解决方法是安排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活动内容(简化):

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
    }
}

解决方法

我在使用爆炸活动转换时也面临同样的问题,我发现这个代码在Lollipop版本中正常运行,但在Lollipop上面崩溃了.
即使我找不到崩溃的原因.但我以另一种方式解决它.只是延迟活动过渡.我在下面给出了我的代码
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());
}}

在所有版本中正常添加功能后.

原文链接:https://www.f2er.com/java/130195.html

猜你在找的Java相关文章