来自另一个片段的FragmentTransaction调用使活动变量为null

时间:2016-03-03 05:32:49

标签: android android-fragments xamarin fragmenttransaction

使用ValueAnimator和自定义ValueAnimator.Update方法对背景进行动画处理。在活动onCreate方法中,使用FragmentTransaction第一个片段调用。从第一个片段开始,基于用户按钮单击调用另一个FragmetTranstaction。在第一个事务中,一切都很完美,但在第二个事务中,ValueAnimator.Update中使用的活动变量为空。问题在哪里?

活动onCreate

    private const int TO_LEFT = 0;
    private const int TO_RIGHT = 1;
    private const long DURATION = 15000;

    private int bgAnimDir = TO_LEFT;
    private RectF mDisplayRect = new RectF();
    private Matrix matrix = new Matrix();
    private ValueAnimator bgAnimator;

    [BindView(Resource.Id.background)]
    ImageView background;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        Cheeseknife.Bind(this);

        background.Post(() =>
        {
            animate();
        });


        fragmentTransaction = FragmentManager.BeginTransaction();
        fragmentTransaction.Add(Resource.Id.home_container, new Home(), "Home");
        fragmentTransaction.Commit();
    }

为背景和矩阵变量引发了空指针异常

animate()功能:

    private void animate()
    {
        updateDisplayRect();
        if (bgAnimDir == TO_LEFT)
            animate(mDisplayRect.Left, mDisplayRect.Left - (mDisplayRect.Right - background.Width));
        else
            animate(mDisplayRect.Left, 0.0f);
    }

    private void animate(float from, float to)
    {
        bgAnimator = ValueAnimator.OfFloat(from, to);
        bgAnimator.Update +=
            (object sender, ValueAnimator.AnimatorUpdateEventArgs e) =>
            {
                float value = (float)e.Animation.AnimatedValue;
                matrix.Reset();
                matrix.PostTranslate(value, 0);
                background.ImageMatrix = matrix;
            };
        bgAnimator.SetDuration(DURATION);
        bgAnimator.AnimationEnd +=
            (object sender, EventArgs e) =>
            {
                bgAnimDir = bgAnimDir == TO_LEFT ? TO_RIGHT : TO_LEFT;
                animate();
            };
        bgAnimator.Start();
    }

bgAnimator.Update代理

中引发的例外情况

1 个答案:

答案 0 :(得分:0)

我通过使用try,catch解决了这个问题。

这是我的新动画功能:

    private void animate(float from, float to)
    {
        bgAnimator = ValueAnimator.OfFloat(from, to);
        bgAnimator.Update +=
            (object sender, ValueAnimator.AnimatorUpdateEventArgs e) =>
            {
                try
                {
                    float value = (float)e.Animation.AnimatedValue;
                    matrix.Reset();
                    matrix.PostTranslate(value, 0);
                    background.ImageMatrix = matrix;
                }
                catch
                {
                    background = (ImageView)FindViewById(Resource.Id.background);
                }
            };
        bgAnimator.SetDuration(DURATION);
        bgAnimator.AnimationEnd +=
            (object sender, EventArgs e) =>
            {
                bgAnimDir = bgAnimDir == TO_LEFT ? TO_RIGHT : TO_LEFT;
                animate();
            };
        bgAnimator.Start();
    }

    private void updateDisplayRect()
    {
        try
        {
            mDisplayRect.Set(0, 0, background.Drawable.IntrinsicWidth, background.Drawable.IntrinsicHeight);
            matrix.MapRect(mDisplayRect);
        }
        catch
        {
            background = (ImageView)FindViewById(Resource.Id.background);
        }
    }

只需在需要时填充null变量!

但正如你猜测的那样,这不是一个简短的答案,可以解释为什么背景在这个过程中变为空。!