I am trying to start an animation when the fragment opens. I don't want it to start with an onClick, but by itself. Tried it in onCreate, but it just crashes the app. I also wanted to try the method: onWindowFocusChanged() but its complicated to use in fragments.
Fragment 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)