在我的Android应用程序中,我有一个启动屏幕,我在那里做一些设置和加载。我的应用程序使用默认的explode
作为windowEnterTransition
和windowExitTransition
以及changeImageTransform
加上changeBounds
转换集作为windowSharedElementEnterTransition
和windowSharedElementExitTransition
。为了方便起见,我使用静态方法启动下一个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
}
}
2条答案
按热度按时间lvmkulzt1#
我也面临着同样的问题,而工作与爆炸活动过渡,我发现一件事,这段代码正常运行在棒棒糖版本,但崩溃以上棒棒糖。连我都找不出崩溃的原因。但我用另一种方式解决它。只需在活动转换中添加延迟。我在下面给出了代码
添加此功能后,在所有版本中正常工作。
x33g5p2x2#
您可以使用根视图
post
方法导航到下一个活动,而不是使用Handler通常,根视图可以是
android.R.id.content