首页 文章

Android studio-I 'm having hard time figuring out why the onTouchtEvent doesn' t识别手指动作 . 有什么建议?

提问于
浏览
0

我正在 Build 一个战舰Android游戏,我很难弄清楚为什么onTouchtEvent无法识别手指移动 .

有什么建议?

package com.example.omer.battleship;

import android.content.Context;

import android.support.v7.app.AppCompatActivity;
import android.view.*;
import android.widget.*;


import java.util.*;


public class Game  extends AppCompatActivity {

    public static GameCell[][] gameGrid ;
    View gameBoard = null;
    public static boolean gameStarted = false;
    Spinner shipSpinner;
    String[] shipsArray;
    Map<String, Integer> shipsMap = new HashMap<String, Integer>();
    private boolean ture;
    private int dimention;
    // ArrayAdapter<String> spinnerArrayAdapter;


    public Game(Context context,int dimention) {
        super();
        this.dimention=dimention;
        initializeApp(dimention);
        gameBoard= findViewById(R.id.boardView);
        gameBoard.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {

                int eventAction=event.getAction();
                switch (eventAction)
                {
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                }
                return true;
            }
        });

}




    private void initializeApp(int dimention) {
        // Initialize the gameGrid
        gameGrid= new GameCell[dimention][dimention];
        for (int y = 0; y < dimention; y++) {
                for (int x = 0; x < dimention; x++) {
                    gameGrid[x][y] = new GameCell();
                }
        }
        gameGrid[1][1].setHas_ship(true);   // For debugging
        gameGrid[1][2].setHit(true);

    }

这是董事会视图类

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;


import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


public class BoardView extends View {
    int border = 10;    // Width of the outer border
    int topBoardX = border;
    int topBoardY = 50;
    int fontSize = 30;
    private int dimention;
    Point[][] grid = new Point[11][11];
    String[] letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J","K","L","M","N","O"};
    String[] numbers = {" 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10"," 11"," 12"," 13"," 14"," 15"};
    //private static Paint paint;
    Point origin = new Point(this.getLeft(), this.getTop());
    Paint paint = new Paint();

    public BoardView(Context context,int dimention) {
        super(context);
        this.dimention=dimention;
        Log.println(Log.ASSERT,"DIMENTION", String.valueOf(dimention));
        init(null);
    }

    public BoardView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        // paint = new Paint();
        paint.setStrokeWidth(10);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setTextSize(fontSize);
    }

    public BoardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);

    }

    public BoardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);

    }

    private void init(@Nullable AttributeSet set) {


    }

    @Override
    protected void onDraw(Canvas canvas) {
        Game game = new Game(this.getContext(),this.dimention);

        super.onDraw(canvas);

        int screenHeight = this.getMeasuredHeight();
        int screenWidth = this.getMeasuredWidth();
        int middle = screenHeight / 2;


        canvas.drawRect(topBoardX, topBoardY, screenWidth - border, middle - border, paint);
        paint.setColor(Color.LTGRAY);

        // Calculate cell width based on width
        int cellWidth = (screenWidth / dimention) - 1;
        int cellHeight = (middle - border) / 12;   // 10 cells + 1 for letters and 1 for Attack/Defend Boards

        // Draw a border
        canvas.drawRect(topBoardX, topBoardY, screenWidth - border, middle - border, paint);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3);


        Game.gameGrid[0][0].setCellHeight(cellHeight);
        Game.gameGrid[0][0].setCellWidth(cellWidth);
        Game.gameGrid[0][0].setViewOrigin(origin);


        for (int y = 0; y < dimention; y++) {
            for (int x = 0; x < dimention; x++) { // TODO:  add the points to the gameGrid
                Game.gameGrid[x][y].setTopleft(new Point(x * cellWidth + border, y * cellHeight + topBoardY));
                Game.gameGrid[x][y].setBottomright(new Point((x + 1) * cellWidth + border, (y + 1) * cellHeight + topBoardX));
            }
        }

        // Draw the horizontal lines
        for (int i = 0; i < dimention; i++) {
            canvas.drawLine(i * cellWidth + border, topBoardY, i * cellWidth + border, middle - border, paint);    // Vertical Lines
            canvas.drawLine(border, i * cellHeight + topBoardY, screenWidth - border, i * cellHeight + topBoardY, paint);  // Horizontal Lines
        }

        //---------------------
        paint.setStrokeWidth(2);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawText("Attacking Board", 40, 40, paint);

        float w = paint.measureText(numbers[0], 0, numbers[0].length());
        float center = (cellWidth / 2) - (w / 2);

        /*drawing the first colum and Row */

        for (int x = 0; x < dimention-1; x++) {
            canvas.drawText(letters[x], Game.gameGrid[x][0].getTopleft().x + center + cellWidth, Game.gameGrid[x][0].getTopleft().y + fontSize + border, paint);
        }
        for (int y = 0; y < dimention-1; y++) {
            canvas.drawText(numbers[y], Game.gameGrid[0][y].getTopleft().x + center, Game.gameGrid[0][y + 1].getTopleft().y + cellHeight - border, paint);
        }

        paint.setColor(Color.WHITE);


        // Draw the contents of the grid
        for (int y = 0; y < dimention; y++) {
            for (int x = 0; x < dimention; x++) {
                if (Game.gameGrid[x][y].getHas_ship())
                    drawCell("S", x, y, center, canvas);
                if (Game.gameGrid[x][y].getWaiting())
                    drawCell("W", x, y, center, canvas);
                if (Game.gameGrid[x][y].getMiss())
                    drawCell("M", x, y, center, canvas);
                if (Game.gameGrid[x][y].getHit())
                    drawCell("H", x, y, center, canvas);
            }

        }
    }

    void drawCell( String contents, int x, int y, float center, Canvas canvas ) {
        canvas.drawText( contents, Game.gameGrid[x][y].getTopleft().x + center, Game.gameGrid[x][y].getTopleft().y + center, paint );
    }

    }

这就像当我触摸一个细胞时,它需要去触摸它并且什么都不做 . 有人能帮我解决问题吗?

这是错误日志

memtrack: Couldn't load memtrack module (No such file or directory)
2018-12-10 12:51:40.764 4616-4616/? E/android.os.Debug: failed to load memtrack module: -2
2018-12-10 12:51:41.241 4635-4635/? E/memtrack: Couldn't load memtrack module (No such file or directory)
2018-12-10 12:51:41.241 4635-4635/? E/android.os.Debug: failed to load memtrack module: -2
2018-12-10 12:51:44.956 4649-4649/? E/memtrack: Couldn't load memtrack module (No such file or directory)
2018-12-10 12:51:44.956 4649-4649/? E/android.os.Debug: failed to load memtrack module: -2
2018-12-10 12:51:45.010 4647-4647/? E/memtrack: Couldn't load memtrack module (No such file or directory)
2018-12-10 12:51:45.010 4647-4647/? E/android.os.Debug: failed to load memtrack module: -2
2018-12-10 12:51:45.297 4670-4670/? E/memtrack: Couldn't load memtrack module (No such file or directory)
2018-12-10 12:51:45.297 4670-4670/? E/android.os.Debug: failed to load memtrack module: -2
2018-12-10 12:51:46.141 1317-1332/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2018-12-10 12:51:50.130 4679-4679/com.example.omer.battleship A/DIMENTION: 8
2018-12-10 12:51:50.244 4679-4679/com.example.omer.battleship E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.omer.battleship, PID: 4679
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
        at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
        at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:56)
        at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
        at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
        at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
        at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
        at com.example.omer.battleship.Game.<init>(Game.java:30)
        at com.example.omer.battleship.BoardView.onDraw(BoardView.java:64)
        at android.view.View.draw(View.java:17071)
        at android.view.View.updateDisplayListIfDirty(View.java:16053)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.view.View.updateDisplayListIfDirty(View.java:16048)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.view.View.updateDisplayListIfDirty(View.java:16048)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.view.View.updateDisplayListIfDirty(View.java:16048)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.view.View.updateDisplayListIfDirty(View.java:16048)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.view.View.draw(View.java:17074)
        at com.android.internal.policy.DecorView.draw(DecorView.java:751)
        at android.view.View.updateDisplayListIfDirty(View.java:16053)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:656)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:662)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:770)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2796)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2604)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2211)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
        at android.view.Choreographer.doCallbacks(Choreographer.java:683)
        at android.view.Choreographer.doFrame(Choreographer.java:619)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

1 回答

  • 1

    您没有为任何单元格设置TouchListener . 虽然您确实编写了onTouchEvent方法,但它并没有附加到要侦听的任何内容上 .

    I.E.

    View myView = findViewById(R.id.my_view);
    myView.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        // ... Respond to touch events
        return true;
    }});
    

相关问题