-1

I have a navigation drawer in my APP.I want to create a slideshow at navigation drawer header. So, iam using ViewFlipper for nav_header.xml. I have used the following code in MainActivity. But the app gets stopped. What changes should i do ?

Getting this error :

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.myapp.praxi/com.myapp.praxi.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

Here is my code :

int[] resources = {R.drawable.prxi,
        R.drawable.prxi1,
        R.drawable.prxi2,
        R.drawable.prxi3,
};

ViewFlipper imageFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);

for (int i = 0; i < resources.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(resources[i]);
            imageFlipper.addView(imageView);
        }



    imageFlipper.setFlipInterval(4000);

    imageFlipper.setInAnimation(this, android.R.anim.slide_in_left); //use either the default slide animation in sdk or create your own ones
    imageFlipper.setOutAnimation(this, android.R.anim.slide_out_right);

    imageFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {

        public void onAnimationStart(Animation animation) {}
        public void onAnimationRepeat(Animation animation) {}
        public void onAnimationEnd(Animation animation) {

            int displayedChild = imageFlipper.getDisplayedChild();
            int childCount = imageFlipper.getChildCount();

            if (displayedChild == childCount - 1) {
                imageFlipper.stopFlipping();
            }
        }
    });

    imageFlipper.startFlipping();`

nav_header File :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="@android:dimen/thumbnail_height"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:orientation="vertical"
    android:gravity="bottom"
    android:background="@drawable/prxi"
    android:id="@+id/linear">

    <ViewFlipper
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/view_flipper">

    </ViewFlipper>

</LinearLayout>
bubsoncode
  • 11
  • 5

1 Answers1

0

The ViewFlipper is a child of element of drawer layout. You need to find the view from the navigation drawer's layout and not from the Activity's view layout.

For example, if this is your drawer layout, you'll have to instantiate the ViewFlipper like this:

DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ViewFlipper imageFlipper = (ViewFlipper) mDrawerLayout.findViewById(R.id.view_flipper);

Hope that helps.

Joel Fernandes
  • 4,776
  • 2
  • 26
  • 48