草庐IT

android - Lollipop 设备的共享元素转换崩溃

coder 2023-12-06 原文

我正在使用 ActivityOptions.makeSceneTransitionAnimation API 来处理共享转换。

我面临的问题是它在 Marshmellow 设备上运行但在 Lolipop 设备上崩溃。

这是崩溃日志:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getScrollX()' on a null object reference
   at android.app.ActivityTransitionCoordinator.setSharedElementState(ActivityTransitionCoordinator.java:435)
   at android.app.ActivityTransitionCoordinator.setSharedElementState(ActivityTransitionCoordinator.java:473)
   at android.app.EnterTransitionCoordinator.startSharedElementTransition(EnterTransitionCoordinator.java:332)
   at android.app.EnterTransitionCoordinator.access$200(EnterTransitionCoordinator.java:42)
   at android.app.EnterTransitionCoordinator$5$1.run(EnterTransitionCoordinator.java:389)
   at android.app.ActivityTransitionCoordinator.startTransition(ActivityTransitionCoordinator.java:698)
   at android.app.EnterTransitionCoordinator$5.onPreDraw(EnterTransitionCoordinator.java:386)
   at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1990)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1088)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5825)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:769)
   at android.view.Choreographer.doCallbacks(Choreographer.java:582)
   at android.view.Choreographer.doFrame(Choreographer.java:552)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:755)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5237)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)

这是我的代码

 @Override
public void getComments(String postId, String authorName, String message, String imageURL, String likesCount, RelativeLayout imageView) {


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && imageView != null) {
        ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(getActivity(), imageView, "revealLayout");

        Intent commentIntent = new Intent(getActivity(), CommentsActivity.class);
        commentIntent.putExtra("postId", postId);
        commentIntent.putExtra("authorName", authorName);
        commentIntent.putExtra("message", message);
        commentIntent.putExtra("imageURL", imageURL);
        commentIntent.putExtra("likesCount", likesCount);
        startActivity(commentIntent, options.toBundle());

    } else {
        Intent commentIntent = new Intent(getActivity(), CommentsActivity.class);
        commentIntent.putExtra("postId", postId);
        commentIntent.putExtra("authorName", authorName);
        commentIntent.putExtra("message", message);
        commentIntent.putExtra("imageURL", imageURL);
        commentIntent.putExtra("likesCount", likesCount);
        startActivity(commentIntent);
    }


}

我通过接口(interface)从适配器回调中获取上述数据

我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/root_layout"
  android:layout_width="match_parent"
  android:transitionName="revealLayout"
  android:layout_height="wrap_content"
  android:background="?android:attr/selectableItemBackground"
  android:orientation="vertical">

在接收端与 android:transitionName 相同的相对布局

这是我的 styles.xml

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">#c65643</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowSharedElementReenterTransition">@null</item>
    <item name="android:windowReenterTransition">@null</item>
    <item name="android:windowAllowReturnTransitionOverlap">false</item>
</style>

还有styles.xml(21)

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">#c65643</item>
            <item name="colorAccent">@color/colorAccent</item>
            <item name="android:windowContentTransitions">true</item>
            <item name="android:windowSharedElementReenterTransition">@null</item>
            <item name="android:windowReenterTransition">@null</item>
            <item name="android:windowAllowReturnTransitionOverlap">false</item>

    </style>

也通过这个链接https://code.google.com/p/android/issues/detail?id=224270&sort=-id&colspec=ID但没有运气

提前致谢

最佳答案

很明显是Android 5.0的Bug,5.1修复了。 尝试将除ScaleType.MATRIX之外的其他ScaleType设置为Transition ImageView

Android 5.0上的ActivityTransitionCoordinator.java源码

537    protected ArrayList<View> createSnapshots(Bundle state, Collection<String> names) {
538      ...
549        for (String name: names) {
550            Bundle sharedElementBundle = state.getBundle(name);
551            if (sharedElementBundle != null) {
552               ...
557                if (snapshot != null) {
558                    setSharedElementState(snapshot, name, state, null, null, decorLoc);
559                }
560                snapshots.add(snapshot);
561            }
562        }
563        return snapshots;
564    }

Android 5.1 上的 ActivityTransitionCoordinator.java 源代码

629    protected ArrayList<View> createSnapshots(Bundle state, Collection<String> names) {
630       ...
641        Matrix tempMatrix = new Matrix();
642        for (String name: names) {
643            Bundle sharedElementBundle = state.getBundle(name);
644            View snapshot = null;
645            if (sharedElementBundle != null) {
646              ...
650                if (snapshot != null) {
651                    setSharedElementState(snapshot, name, state, tempMatrix, null, decorLoc);
652                }
653            }
655            snapshots.add(snapshot);
656        }
657        return snapshots;
658    }

以及ActivityTransitionCoordinator.setSharedElementState中的崩溃线

376    private void setSharedElementState(View view, String name, Bundle transitionArgs,
377            Matrix tempMatrix, RectF tempRect, int[] decorLoc) {
378        Bundle sharedElementBundle = transitionArgs.getBundle(name);
379        if (sharedElementBundle == null) {
380            return;
381        }
382
383        if (view instanceof ImageView) {
384            int scaleTypeInt = sharedElementBundle.getInt(KEY_SCALE_TYPE, -1);
385            if (scaleTypeInt >= 0) {
386                ImageView imageView = (ImageView) view;
387                ImageView.ScaleType scaleType = SCALE_TYPE_VALUES[scaleTypeInt];
388                imageView.setScaleType(scaleType);
389                if (scaleType == ImageView.ScaleType.MATRIX) {
390                    float[] matrixValues = sharedElementBundle.getFloatArray(KEY_IMAGE_MATRIX);
391                    tempMatrix.setValues(matrixValues);
392                    imageView.setImageMatrix(tempMatrix);
393                }
394            }
395        }
396        ...
450    }

关于android - Lollipop 设备的共享元素转换崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40039803/

有关android - Lollipop 设备的共享元素转换崩溃的更多相关文章

  1. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  2. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  3. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  4. ruby - 将散列转换为嵌套散列 - 2

    这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[

  5. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  6. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

  7. Ruby Readline 在向上箭头上使控制台崩溃 - 2

    当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby​​安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少

  8. ruby-on-rails - Ruby url 到 html 链接转换 - 2

    我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.

  9. ruby-on-rails - 使用 ruby​​ 将多个实例变量转换为散列的更好方法? - 2

    我收到格式为的回复#我需要将其转换为哈希值(针对活跃商家)。目前我正在遍历变量并执行此操作:response.instance_variables.eachdo|r|my_hash.merge!(r.to_s.delete("@").intern=>response.instance_eval(r.to_s.delete("@")))end这有效,它将生成{:first="charlie",:last=>"kelly"},但它似乎有点hacky和不稳定。有更好的方法吗?编辑:我刚刚意识到我可以使用instance_variable_get作为该等式的第二部分,但这仍然是主要问题。

  10. ruby - 在哈希的键数组中追加元素 - 2

    查看我的Ruby代码:h=Hash.new([])h[0]=:word1h[1]=h[1]输出是:Hash={0=>:word1,1=>[:word2,:word3],2=>[:word2,:word3]}我希望有Hash={0=>:word1,1=>[:word2],2=>[:word3]}为什么要附加第二个哈希元素(数组)?如何将新数组元素附加到第三个哈希元素? 最佳答案 如果您提供单个值作为Hash.new的参数(例如Hash.new([]),完全相同的对象将用作每个缺失键的默认值。这就是您所拥有的,那是你不想要的。您可以改用

随机推荐