在CubeLayout类中。我添加了一个CubeCompleted接口
公共类CubeLayout扩展了FrameLayout {
private BaseInterpolator mInterpolator = new AccelerateDecelerateInterpolator();
public CubeLayout(Context context) {
this(context, null);
}
public CubeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CubeLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
View foregroundView = getChildAt(0);
View backgroundView = getChildAt(1);
CubeLeftOutAnimation cubeLeftOutAnimation = new CubeLeftOutAnimation();
cubeLeftOutAnimation.setDuration(1000);
cubeLeftOutAnimation.setFillAfter(true);
CubeRightInAnimation cubeRightInAnimation = new CubeRightInAnimation();
cubeRightInAnimation.setDuration(1000);
cubeRightInAnimation.setFillAfter(true);
foregroundView.startAnimation(cubeLeftOutAnimation);
backgroundView.startAnimation(cubeRightInAnimation);
cubeRightInAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
completed.completedCube();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
public interface CubeCompleted {
public void completedCube();
}
CubeCompleted completed;
}
在我的活动中。我有替换片段
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.replace(R.id.content_main, mHomeFragment)
.commit();
}
在MyFragment中。我覆盖了completedCube
public class HomeFragment extends Fragment implements CubeLayout.CubeCompleted {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
return view;
}
@Override
public void completedCube() {
// donutProgress();
}
}
这是我在Logcat中的Bug
Process: com.seesaa.newsaudiocast, PID: 31189
java.lang.NullPointerException: Attempt to invoke interface method 'void com.view.CubeLayout$CubeCompleted.completedCube()' on a null object reference
at com.seesaa.newsaudiocast.view.CubeLayout$1.onAnimationEnd(CubeLayout.java:56)
at android.view.animation.Animation$3.run(Animation.java:374)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
答案 0 :(得分:1)
编辑:我认为你正试图实现某种回调,所以试试这个 -
在CubeLayout
课程中,添加此内容 -
public void setCallBack(CubeCompleted completed) {
this.completed = completed;
}
在您的片段中,使用findViewById
找到您的CubeLayout
,并为其添加一个监听器 -
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
CubeLayout cubelayout = view.findViewById(R.id.cubeLayout);
cubeLayout.setCallBack(this);
return view;
}
答案 1 :(得分:0)
在调用任何方法之前,您需要初始化completed
对象。
答案 2 :(得分:0)
public class HomeFragment extends Fragment implements CubeLayout.CubeCompleted {
private CubeCompleted cubeCompletedInterface;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
cubeCompletedInterface=getActivity();
return view;
}
@Override
public void completedCube() {
// donutProgress();
}
}
/ *你的动画类删除CubeCompleted完成; * /
private BaseInterpolator mInterpolator = new AccelerateDecelerateInterpolator();
public CubeLayout(Context context) {
this(context, null);
}
public CubeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CubeLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
View foregroundView = getChildAt(0);
View backgroundView = getChildAt(1);
CubeLeftOutAnimation cubeLeftOutAnimation = new CubeLeftOutAnimation();
cubeLeftOutAnimation.setDuration(1000);
cubeLeftOutAnimation.setFillAfter(true);
CubeRightInAnimation cubeRightInAnimation = new CubeRightInAnimation();
cubeRightInAnimation.setDuration(1000);
cubeRightInAnimation.setFillAfter(true);
foregroundView.startAnimation(cubeLeftOutAnimation);
backgroundView.startAnimation(cubeRightInAnimation);
cubeRightInAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
completed.completedCube();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
public interface CubeCompleted {
public void completedCube();
}
}