1

I am implementing swipe action in my code to change background images. But when I run my app its crash. I dont know whats wrong in my code.

Code-

public class Types extends Activity{

    RelativeLayout layout1;
    int[] backgroundResId;
    int currentIndex=0;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_type);
        backgroundResId=new int[]{R.drawable.back,R.drawable.bg,R.drawable.frame1};
        changeBackground();
        ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this);
        layout1.setOnTouchListener(activitySwipeDetector);

    }


    private void changeBackground(){
         findViewById(R.id.layout1).setBackgroundResource(backgroundResId[currentIndex]);
    }

    public class ActivitySwipeDetector implements View.OnTouchListener {

        static final String logTag = "ActivitySwipeDetector";
        static final int MIN_DISTANCE = 100;
        private float downX, upX;
        private Activity activity;

        public ActivitySwipeDetector(Activity activity){
            this.activity = activity;
        }

        public void onRightToLeftSwipe(){
            Log.i(logTag, "RightToLeftSwipe!");
            currentIndex++;
            if(currentIndex<backgroundResId.length){
                 changeBackground();
            }
        }

        public void onLeftToRightSwipe(){
            Log.i(logTag, "LeftToRightSwipe!");
            if(currentIndex>0){
                changeBackground();
           }
        }

        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN: {
                    downX = event.getX();
                    return true;
                }
                case MotionEvent.ACTION_UP: {
                    upX = event.getX();

                    float deltaX = downX - upX;

                    // swipe horizontal?
                    if(Math.abs(deltaX) > MIN_DISTANCE){
                        // left or right
                        if(deltaX < 0) { this.onLeftToRightSwipe(); return true; }
                        if(deltaX > 0) { this.onRightToLeftSwipe(); return true; }
                    }
                    else {
                            Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                            return false; // We don't consume the event
                    }


                    return true;
                }
            }
            return false;
        }

        }

}
John R
  • 2,078
  • 8
  • 35
  • 58

2 Answers2

3
RelativeLayout layout1;

You have declared but didn't initialize the layout1, initialize it with the id and try.

Tamilselvan Kalimuthu
  • 1,534
  • 1
  • 13
  • 28
  • swipe from right to left work but form left to right not work. – John R Jan 14 '14 at 08:43
  • @JohnR this should be sepearate question. any how see this link having code for both swipe from left to right and right to left.http://stackoverflow.com/a/12938787/1937802 – Tamilselvan Kalimuthu Jan 14 '14 at 08:45
  • In my code right to left swipe working if i take reference from the link u provide i need to change code. But i dont want to do that. – John R Jan 14 '14 at 08:52
  • You should use GestureListener use the link that i provided , nothing to change u can use the code in that example – Tamilselvan Kalimuthu Jan 14 '14 at 08:59
  • one more idea it may be use full for u. keep the if condition as you have and inside the if condition check another one condition that if(upx is > downX ) then it might be left to right swipe else if should be right to left swipe, let me know if u try this. – Tamilselvan Kalimuthu Jan 14 '14 at 09:08
0

You must be getting the NullPointerException, because you haven't initialized the RelativeLayout instance layout1. Do it like this-

private void changeBackground()
{
     layout1 = findViewById(R.id.layout1);
     layout1.setBackgroundResource(backgroundResId[currentIndex]);
}
amit singh
  • 1,407
  • 2
  • 16
  • 25