Android 打开片段时开始动画

Android start animation when opening fragment

我正在尝试在片段打开时启动动画。我不希望它以 onClick 开始,而是单独开始。在 onCreate 中尝试过,但它只会使应用程序崩溃。我也想试试这个方法:onWindowFocusChanged() 但它在片段中使用起来很复杂。

片段JAVAclass:

    package com.example.appsplashscreen;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class SecondFragment extends Fragment implements View.OnClickListener {

    // Attributes
    private ImageView image;
    private ImageButton button;
    private Animation moveAnimation;

    public SecondFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_second, container, false);

        button = v.findViewById(R.id.button);
        button.setOnClickListener(this);

        image = (ImageView) getView().findViewById(R.id.imageView4);
        moveAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.move_animation);
        image.startAnimation(moveAnimation);

        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button :
                // your button click
                button.setVisibility(View.GONE);
                break;
        }
    }
}

错误消息:这是应用程序崩溃时的错误消息。:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.appsplashscreen, PID: 32704
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
    at com.example.appsplashscreen.SecondFragment.onCreateView(SecondFragment.java:49)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
    at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
    at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
    at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
    at androidx.fragment.app.FragmentManagerImpl.run(FragmentManagerImpl.java:150)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

这个post会添加一些颜色: getView returning null when fragment has been created from an activity

但本质上是 getView() returns onCreateView 返回的视图,因此在此方法之前它将为空。

您应该使用充气机解析的视图来解析您的组件