1

I made the snake and ladder game in android studio. I want to change the height and width of the buttons, and I want to add one more button for exit. Right now I am using only one .png file for all buttons. So, How to change it? When i add the fourth button my application crash and its shows the error. Check this image

Error

    FATAL EXCEPTION: main
    Process: com.sixtinbyte.snakeandladder, PID: 25115
 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.sixtinbyte.snakeandladder.components.GameButton.onDraw(android.graphics.Canvas)' on a null object reference
 at com.sixtinbyte.snakeandladder.HomeView.onDraw(HomeView.java:151)

HomeView. java (This are the java file where i want to change because this is the homescreen of the game)

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;

import com.sixtinbyte.snakeandladder.components.GameButton;
import com.sixtinbyte.snakeandladder.constant.Sound;


public class HomeView extends View {
 public Context ctx;
 private Resources res;
 private String refText ="Snakes and Ladders";
 private GameButton gButton, sButton, hButton,eButton;
 private Bitmap logo, play, settings, help, exit;
    private int hWidth, hHeight;
 private Point touchCoordinate = new Point();


 public HomeView(Context ctx) {
    super(ctx);
    this.ctx = ctx;
    res = getResources();
    logo = BitmapFactory.decodeResource(res, R.drawable.snakes_n_ladders);

 }

 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    // TODO: Implement this method
    super.onSizeChanged(w, h, oldw, oldh);
    hWidth = w;
    hHeight = h;
    preparePlayButton();
    prepareSettingsButton();
    prepareHelpButton();
//   prepareExitButton();
 }

    private void preparePlayButton() {
    String text =" ";
    int textSize = 40;
    Point po = getBmpMeasureFrom(text, refText, textSize);
    play = getBitmap(R.drawable.play, po.x, po.y);
    gButton = new GameButton(play, text, textSize);
    gButton.setLocation((hWidth - po.x) / 2, (int)(hHeight * 0.6f));
    gButton.setTextColor(Color.CYAN);
 }
 private void prepareSettingsButton() {
    String text =" ";
    int textSize = 40;
    Point po = getBmpMeasureFrom(text, refText, textSize);
    settings = getBitmap(R.drawable.play, po.x, po.y);
    sButton = new GameButton(settings, text, textSize);
    sButton.setLocation((hWidth - po.x) / 2, (int)(hHeight * 0.7f));
    sButton.setTextColor(Color.CYAN);
 }
//
//
 private void prepareHelpButton() {
    String text =" ";
    int textSize = 40;
    Point po = getBmpMeasureFrom(text, refText, textSize);
    help = getBitmap(R.drawable.play, po.x, po.y);
    hButton = new GameButton(help, text, textSize);
    hButton.setLocation((hWidth - po.x) / 2, (int)(hHeight * 0.8f));
    hButton.setTextColor(Color.CYAN);
 }
//  private void prepareExitButton() {
//      String text =" ";
//      int textSize = 40;
//      Point po = getBmpMeasureFrom(text, refText, textSize);
//      help = getBitmap(R.drawable.play, po.x, po.y);
//      hButton = new GameButton(help, text, textSize);
//      hButton.setLocation((hWidth - po.x) / 2, (int)(hHeight * 0.8f));
//      hButton.setTextColor(Color.CYAN);
//
//  }

 private Point getBmpMeasureFrom(String text, String refText, int textSize) {
    Point p = new Point();
    Paint pa = new Paint();
    pa.setTextSize(textSize);
    p.y = (int)(pa.getTextSize() * 2.2f);
    String tt = refText.length() > text.length() ?refText: text;
    p.x = (int) (pa.measureText(tt) * 1.2f);
    return p;
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
    // TODO: Implement this method
    int evt = event.getAction();
    touchCoordinate.x = (int)event.getX();
    touchCoordinate.y = (int)event.getY();
    switch (evt) {
     case MotionEvent.ACTION_DOWN:
 MainActivity.playSound(Sound.BUTTON_CLICK_1, false);
        break;
     case MotionEvent.ACTION_MOVE:

        break;
     case MotionEvent.ACTION_UP:
//      MainActivity.stopSound();
        if (gButton.contains(touchCoordinate)) {
         Intent intent = new Intent(ctx, GameActivity.class);
         ctx. startActivity(intent);
        }
        if (sButton.contains(touchCoordinate)) {

         Intent intent = new Intent(ctx, Playing.class);
            ctx.startActivity(intent);
        }
        if (hButton.contains(touchCoordinate)) {

         Intent intent = new Intent(ctx, Aboutus.class);
         ctx. startActivity(intent);
        }
//       if(eButton.contains(touchCoordinate)){
//           Intent intent = new Intent(ctx, Exit.class);
//           ctx.startActivity(intent);
//       }

        break;
    }
    return true;
 }



 @Override
 protected void onDraw(Canvas canvas) {
    // TODO: Implement this method
    super.onDraw(canvas);
    canvas.drawColor(Color.CYAN);

    canvas.drawBitmap(logo, (hWidth - logo.getWidth()) / 2, (int)((hHeight - logo.getHeight()) * 0.1), null);
    gButton.onDraw(canvas);
    sButton.onDraw(canvas);
    hButton.onDraw(canvas);
//  eButton.onDraw(canvas);
//  Paint p= new Paint();
//  p.setColor(Color.RED);
//  canvas.drawRect(100, 200, 400, 500, p);
    invalidate();
 }

 private Bitmap getBitmap(int drawableRes, int width, int height) {
    Drawable d = res.getDrawable(drawableRes);
    Canvas c = new Canvas();
    Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    c.setBitmap(b);
    d.setBounds(0, 0,width, height);
    d.draw(c);
    return b;
 }

}
  • You have already commented out the code that adds the fourth button, what is the problem there? – Haem Sep 07 '17 at 06:57
  • I commented on the fourth button because my application getting crash and its changing the layout of the button that's why i commented it. –  Sep 07 '17 at 06:59
  • I see. It seems that this question should be split to several questions: One question about the crash and another about changing the button size. (Also, see [this question](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) about asking for crashes) – Haem Sep 07 '17 at 07:02

1 Answers1

0

The prepareExitButton method is assigning hButton, I think it should be assigning eButton. That's why you're getting a NullPointerException there. Also, you're setting the y-coordinate to (int)(hHeight * 0.8f)) which means it will overlap with the help-button.

You may need to alter the GameButton class to get the ability to adjust the button's size. In particular, you'll want to change onDraw to draw the button in the specified size.

Haem
  • 929
  • 6
  • 15
  • 31