2016-02-19 2 views
0

Я пробовал это некоторое время, но я не могу повернуть вид фрагмента, который находится внутри viewpager. Щелчок находится в представлении родительского фрагмента viewpager. Я получаю исключение из null-указателя. Я прокомментировал выше строку кода, на которой я получаю исключение.Flip Animation На вид внутри viewpager android

MainFragment

 int item = mViewPager.getCurrentItem(); 
      Fragment frag = (Fragment) mSectionsPagerAdapter.getItem(item); 
      final View containers = frag.getView(); 
      rootView.findViewById(R.id.flipCard).setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Toast.makeText(getActivity().getApplicationContext(), "Working ", Toast.LENGTH_SHORT).show(); 
//**Exception at below line** 
        View rootLayout = (View) containers.findViewById(R.id.card_view); 

        View cardFace = (View) containers.findViewById(R.id.mainLayout); 
        View cardBack = (View) containers.findViewById(R.id.mainLayout2); 
        if (cardFace == null || cardBack == null) { 
         Toast.makeText(getActivity().getApplicationContext(), "Null View", Toast.LENGTH_SHORT).show(); 
        } 
        FlipAnimation flipAnimation = new FlipAnimation(cardFace, cardBack); 

        if (cardFace.getVisibility() == View.GONE) { 
         flipAnimation.reverse(); 
        } 
        rootLayout.startAnimation(flipAnimation); 
       } 
      }); 

FlipAnimation

public class FlipAnimation extends Animation { 
    private Camera camera; 

    private View fromView; 
    private View toView; 

    private float centerX; 
    private float centerY; 

    private boolean forward = true; 

    /** 
    * Creates a 3D flip animation between two views. 
    * 
    * @param fromView First view in the transition. 
    * @param toView Second view in the transition. 
    */ 
    public FlipAnimation(View fromView, View toView) { 
     this.fromView = fromView; 
     this.toView = toView; 

     setDuration(700); 
     setFillAfter(false); 
     setInterpolator(new AccelerateDecelerateInterpolator()); 
    } 

    public void reverse() { 
     forward = false; 
     View switchView = toView; 
     toView = fromView; 
     fromView = switchView; 
    } 

    @Override 
    public void initialize(int width, int height, int parentWidth, int parentHeight) { 
     super.initialize(width, height, parentWidth, parentHeight); 
     centerX = width/2; 
     centerY = height/2; 
     camera = new Camera(); 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     // Angle around the y-axis of the rotation at the given time 
     // calculated both in radians and degrees. 
     final double radians = Math.PI * interpolatedTime; 
     float degrees = (float) (180.0 * radians/Math.PI); 

     // Once we reach the midpoint in the animation, we need to hide the 
     // source view and show the destination view. We also need to change 
     // the angle by 180 degrees so that the destination does not come in 
     // flipped around 
     if (interpolatedTime >= 0.5f) { 
      degrees -= 180.f; 
      fromView.setVisibility(View.GONE); 
      toView.setVisibility(View.VISIBLE); 
     } 

     if (forward) 
      degrees = -degrees; //determines direction of rotation when flip begins 

     final Matrix matrix = t.getMatrix(); 
     camera.save(); 
     camera.rotateY(degrees); 
     camera.getMatrix(matrix); 
     camera.restore(); 
     matrix.preTranslate(-centerX, -centerY); 
     matrix.postTranslate(centerX, centerY); 
    } 
} 

Logcat

неустранимым: Основной Процесс: com.productrx.yelome, ПИД: 2656 java.lang.NullPointerException: Попытка вызвать виртуальный метод 'android.view.View android.view.View.findViewById (INT)' на нуль ссылки на объект в com.productrx.yelome.fragments.ChooseTemplate $ 2 $ 1.onClick (ChooseTemplate.java:106) на android.view.View.performClick (View.java:4855) на android.view.View $ PerformClick.run (Просмотр. java: 20278) на android.os.Handler.handleCallback (Handler.java:815) на android.os.Handler.dispatchMessage (Handler.java:104) на android.os.Looper.loop (Looper.java: 194) at android.app.ActivityThread.main (ActivityThread.java:5631) по адресу java.lang.reflect.Method.invoke (собственный метод) по адресу java.lang.reflect.Method.invoke (Method.java:372) в com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:959) в com.android.internal.os.ZygoteInit.main (ZygoteInit.java:754)

ответ

1

Надень код в oncreateview фрагмента в случае, если у вас его нет, и поэтому вам не нужно вызывать frag.getview(), который возвращает null в вашем случае.

+0

Я хочу получить clicklistener представления в родительском фрагменте и на этом клике я хочу, чтобы представление внутри фрагмента viewpager перевернулось, вот где я застрял. Я уже пробовал ваше решение, но я не получаю доступ к просмотру за пределами viewpager. Спасибо – Aakash

+0

Вы можете сделать интерфейс и реализовать его в классе, содержащем viewpager. При щелчке по вызову вызовите метод интерфейса. –

+0

Я понял, спасибо, спасибо :) – Aakash