dimanche 8 novembre 2015

Shared preferences + Scrollable slider

I am having a troublesome and tricky activity where: -IF it is the first time the app is opened, it setContentView to a layout named flipper, and -ELSE if starts the intent of going to the activity MainPage.Class

Now, I get a NULLPointer exception when I want to swipe from right to left and set the IF(viewFlipper.getDisplayedChild() == 1)

My activity is the following:

public class Tutorial extends MainActivity{

private ViewFlipper viewFlipper;
private float lastX;


protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if(!prefs.getBoolean("first_time", false))
    {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("first_time", true);
        editor.commit();
        Intent i = new Intent(Tutorial.this, MainPage.class);
        this.startActivity(i);
    }
    else
    {

        setContentView(R.layout.flipper);
    }

}

public boolean onTouchEvent(MotionEvent touchevent)
{
    switch (touchevent.getAction())
    {

        case MotionEvent.ACTION_DOWN:
        {
            lastX = touchevent.getX();
            break;
        }
        case MotionEvent.ACTION_UP:
        {
            float currentX = touchevent.getX();

            if (lastX < currentX)
            {

                if (viewFlipper.getDisplayedChild() == 0)
                    break;

                viewFlipper.setInAnimation(this, R.anim.in_from_left);
                viewFlipper.setOutAnimation(this, R.anim.out_to_right);
                // Show the next Screen
                viewFlipper.showNext();
            }

            if (lastX > currentX)
            {
                if (viewFlipper.getDisplayedChild() == 1)
                    break;

                viewFlipper.setInAnimation(this, R.anim.in_from_right);
                viewFlipper.setOutAnimation(this, R.anim.out_to_left);
                // Show The Previous Screen
                viewFlipper.showPrevious();
            }
            break;
        }
    }
    return false;
}

}

What could be possibly wrong?

Aucun commentaire:

Enregistrer un commentaire