-2

I have use
Android Cube Demo

In CubeLayout class. I add a interface CubeCompleted

public class CubeLayout extends 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;

}

In my Activity. I have Replace a Fragment

@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();

}

In MyFragment. I overide 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();
}
}

This is my Bug in Logcat

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)
Thang BA
  • 162
  • 3
  • 13

3 Answers3

1

Edit: I think you're trying to implement a callback of some kind, so try this out -

In your CubeLayout class, add this -

public void setCallBack(CubeCompleted completed) {
    this.completed = completed;
}

In your fragment, use findViewById to find your CubeLayout, and attach a listener to it -

@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;
}
Rohan
  • 1,180
  • 2
  • 15
  • 28
  • I tried. It not bug. But , At MyFragment. When I override completedCude. It not working @Override public void completedCube() { Toast.make(....); } – Thang BA Mar 22 '16 at 05:51
0

You need to initialize your completed object before calling any methods on it.

Nick Badal
  • 681
  • 1
  • 8
  • 26
  • 1
    If your CubeLayout is being inflated in HomeFragment, you can use `findViewById` to get an instance of it, then use a setter to set your listener. – Nick Badal Mar 22 '16 at 05:40
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();
}
}

/* your animation class remove CubeCompleted completed; */

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



}
Amit Ranjan
  • 567
  • 2
  • 11
  • You remove CubeCompleted completed; Why? @Override public void onAnimationEnd(Animation animation) { completed.completedCube(); } – Thang BA Mar 22 '16 at 05:56
  • can't i initialise from the HomeFragment ?? – Amit Ranjan Mar 22 '16 at 06:01
  • In the `CubeLayout` class, there is no object defined called `completed`. How is this call `completed.completedCube();` supposed to work ? – Rohan Mar 22 '16 at 06:40