简体   繁体   English

Android 打开片段时启动 animation

[英]Android start animation when opening fragment

I am trying to start an animation when the fragment opens.我正在尝试在片段打开时启动 animation。 I don't want it to start with an onClick, but by itself.我不希望它以 onClick 开头,而是单独开始。 Tried it in onCreate, but it just crashes the app.在 onCreate 中尝试过,但它只是使应用程序崩溃。 I also wanted to try the method: onWindowFocusChanged() but its complicated to use in fragments.我也想试试这个方法:onWindowFocusChanged() 但它在片段中使用起来很复杂。

Fragment JAVA class:片段 JAVA class:

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

Errormessage: This is the Errormessage when the app crashes.:错误消息:这是应用程序崩溃时的错误消息。:

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$2.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)

This post will add some color: getView returning null when fragment has been created from an activity这篇文章将添加一些颜色: getView return null when fragment has been created from an Activity

But essentially getView() returns the view returned by onCreateView, so it will be null until after this method.但本质上 getView() 返回的是 onCreateView 返回的视图,所以在这个方法之后它会是 null。

You should use the view resolved by the inflator for resolving your components您应该使用充气机解析的视图来解析您的组件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM