安卓打开碎片时的启动动画

问题描述 投票:0回答:1

我试图在片段打开时启动一个动画。我不想让它在onClick时启动,而是自己启动。在onCreate中试过,但它只是让应用程序崩溃。我还想尝试使用onWindowFocusChanged()这个方法,但它在片段中使用起来很复杂。

片段JAVA类。

    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: 这是应用程序崩溃时的Errormessage。

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)
java android android-fragments animation android-animation
1个回答
1
投票

这个帖子会增加一些色彩。getView在从活动中创建片段时返回null。

但本质上getView()返回的是onCreateView返回的视图,所以在这个方法之后,它将是空的。

你应该使用inflator解析的视图来解析你的组件。

© www.soinside.com 2019 - 2024. All rights reserved.