0

First off, I've been coding for all of a few months, so I'm sorry if any of the following is very basic... I'm pretty terrible with Java.

I'm looking to pick an image from an imaginary "grid/array" based on two variables (x and y, to keep it simple) and display that image, using gestures(fling) to move in between different images. I have no red lines in my code, but everytime I start the Activity I get the error code at the bottom.

I also am not sure how to use Integer.MAX_VALUE and Integer.MIN_VALUE to constrain the two variables so that the app doesn't try to pull for a value that doesn't exist. X can be from 1-5, and y can be from 1-3, and I don't know how to constrain them... filenames are "seat11.png" through "seat53.png".

Here are the relevant code samples:

Setting integers:

private GestureDetectorCompat gestureDetector;
public ImageView seatImage;
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;    
int x = 3;
int y = 1;
String seatNumber = "R.drawable.seat" + x + y;
int seat = Integer.valueOf(seatNumber);

Relevant gesture:

@Override
public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    boolean result = false;
    try {
        float diffY = e2.getY() - e1.getY();
        float diffX = e2.getX() - e1.getX();
        if (Math.abs(diffX) > Math.abs(diffY)) {
            if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                if (diffX > 0) {
                    //swipe right//
                    x = x + 1;
                    seatImage.setImageResource(seat);

                } else {
                    //swipe left//
                    x = x - 1;
                    seatImage.setImageResource(seat);
                }
            }

            result = true;
        }
        else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
            if (diffY > 0) {
                //swipe down//
                y = y - 1;
                seatImage.setImageResource(seat);
            } else {
                //swipe up//
                y = y + 1;
                seatImage.setImageResource(seat);
            }
        }
        result = true;

    } catch (Exception exception) {
        exception.printStackTrace();
    }
    return result;
}

With a double tap to return to the "home" image:

@Override
public boolean onDoubleTap(MotionEvent e) {
    x = 3;
    y = 1;
    seatImage.setImageResource(seat);
    return true;
}

I'm getting the following error text:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.flextronics.doordisplaydemo/com.flextronics.doordisplaydemo.FlexSeat}: java.lang.NumberFormatException: Invalid int: "R.drawable.seat32"
Caused by: java.lang.NumberFormatException: Invalid int: "R.drawable.seat32"

I've tried just about everything I can think of (and find in other answers) and I'm at a total loss. The code provided SEEMS to be the closest to correct that I've gotten.

1 Answers1

0

This here:

String seatNumber = "R.drawable.seat" + x + y;
int seat = Integer.valueOf(seatNumber);

Is trying to convert the string to a number, but that won't work as the string does not contain a number. What you're looking for is this:

int seat = getResources().getIdentifier("seat" + x + y, "drawable", getPackageName());

From: https://stackoverflow.com/a/19093447/360211

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203
  • Now returns the following error code... java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.flextronics.doordisplaydemo/com.flextronics.doordisplaydemo.FlexSeat}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference – Cody Blake Oct 11 '16 at 13:17
  • Make sure that line is inside a method, not outside. In fact you need to re-evaluate that just before you need it. – weston Oct 11 '16 at 14:42
  • This was my problem, with an if/else to handle not going outside the "array," thanks! – Cody Blake Oct 11 '16 at 20:00