为什么这个Android应用程序包在运行时会显示意外错误?

时间:2015-05-04 12:29:49

标签: java android

所有课程都是连续发布的。这个程序是正确的,直到通过创建自定义视图设置“GameActivity”的背景。但是之后当我想在Android设备中添加单个机器人时,它会显示“意外错误”。

WelcomeActivity

package com.emdad.androidapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;


public class WelcomeActivity extends Activity {

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

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_welcome);

    final Button startButton=(Button) findViewById(R.id.androidButton);

    startButton.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {

            switch (arg1.getAction()) {
            case MotionEvent.ACTION_DOWN:

                startButton.getBackground().setAlpha(100);

                break;

            case MotionEvent.ACTION_UP:

                Intent intent=new Intent(WelcomeActivity.this, GameActivity.class);
                startActivity(intent);
                startButton.getBackground().setAlpha(255);

                break;

            case MotionEvent.ACTION_MOVE:

                break;  

            default:
                break;
            }


            return false;
        }
    });


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.welcome, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

GameActivity

package com.emdad.androidapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

public class GameActivity extends Activity {

GameView gameView;

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

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    gameView=new GameView(this);    

    setContentView(gameView);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.game, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

GameView

package com.emdad.androidapp;

import java.util.Random;

import android.content.Context;
import android.graphics.Point;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements Callback{
Context context;
SurfaceHolder surfaceHolder;
DrawingThread drawingThread;

public GameView(Context context) {
    super(context);
    this.context=context;
    surfaceHolder=getHolder();
    surfaceHolder.addCallback(this);

    drawingThread=new DrawingThread(this, context);

}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    try {
        drawingThread.start();
    } catch (Exception e) {
        restartThread();
    }
}

private void restartThread() {
    drawingThread.stopThread();
    drawingThread=null;
    drawingThread=new DrawingThread(this, context);
    drawingThread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    drawingThread.stopThread();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    Point touchPoint=new Point();
    touchPoint=new Point((int)event.getX(), (int)event.getY());
    Random random=new Random();


    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        drawingThread.allRobots.add(new Robot(drawingThread.allPossibleRobots.get(random.nextInt(6)), touchPoint));

        break;
    case MotionEvent.ACTION_UP:

        break;
    case MotionEvent.ACTION_MOVE:

        drawingThread.allRobots.get(drawingThread.allRobots.size()-1).setCenter(touchPoint);

        break;

    default:
        break;
    }
    return super.onTouchEvent(event);
}




}

DrawingThread

package com.emdad.androidapp;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.view.Display;
import android.view.WindowManager;

public class DrawingThread extends Thread{

private Canvas canvas;
private GameView gameView;
private Context context;

boolean threadFlag=false;

Bitmap backgroundBitmap;

int displayX, displayY;

ArrayList<Robot> allRobots;
ArrayList<Bitmap> allPossibleRobots;

public DrawingThread(GameView gameView, Context context) {
    super();
    this.gameView = gameView;
    this.context = context;

    initializaeAll();


}






private void initializaeAll() {

    WindowManager windowManager=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display defaultDisplay=windowManager.getDefaultDisplay();

    Point displayDimension=new Point();
    defaultDisplay.getSize(displayDimension);

    displayX=displayDimension.x;
    displayY=displayDimension.y;

    backgroundBitmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.background1);
    backgroundBitmap=Bitmap.createScaledBitmap(backgroundBitmap, displayX, displayY, true);

    initializeAllPossibleRobots();
}






private void initializeAllPossibleRobots() {
    allRobots=new ArrayList<Robot>();
    allPossibleRobots=new ArrayList<Bitmap>();

    allPossibleRobots.add(giveResizedRobotBitmap(R.drawable.android));
    allPossibleRobots.add(giveResizedRobotBitmap(R.drawable.android2));
    allPossibleRobots.add(giveResizedRobotBitmap(R.drawable.android3));
    allPossibleRobots.add(giveResizedRobotBitmap(R.drawable.android6));
    allPossibleRobots.add(giveResizedRobotBitmap(R.drawable.apple));
    allPossibleRobots.add(giveResizedRobotBitmap(R.drawable.apple2));

}

private Bitmap giveResizedRobotBitmap(int resourceID) {
    Bitmap tempBitmap=BitmapFactory.decodeResource(context.getResources(), resourceID);
    tempBitmap=Bitmap.createScaledBitmap(tempBitmap, displayX/5, (tempBitmap.getHeight()/tempBitmap.getWidth())*(displayX/5), true);

    return tempBitmap;
}





@Override
public void run() {
    threadFlag=true;

    while (threadFlag) {

        canvas=gameView.surfaceHolder.lockCanvas();

        try {
            synchronized (canvas) {

                updateDisplay();

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            if (canvas!=null) {
                gameView.surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }


        try {
            Thread.sleep(17);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }


}


private void updateDisplay() {

    canvas.drawBitmap(backgroundBitmap, 0, 0, null);

    for (int i = 0; i < allRobots.size(); i++) {
        Robot temprRobot=allRobots.get(i);

        canvas.drawBitmap(temprRobot.robotBitmap, temprRobot.centerX-(temprRobot.width/2), temprRobot.centerY-(temprRobot.height/2), temprRobot.robotPaint);
    }
}






public void stopThread() {
    threadFlag=false;
}


}

机器人

package com.emdad.androidapp;

import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Point;

public class Robot {
int centerX, centerY;
int height, width;
Bitmap robotBitmap;
Paint robotPaint;

public Robot(Bitmap bitmap) {
    robotBitmap=bitmap;
    centerX=centerY=0;
    height=robotBitmap.getHeight();
    width=robotBitmap.getWidth();
    robotPaint=new Paint();
}

public Robot(Bitmap bitmap, int cX, int cY) {
    this(bitmap);
    centerX=cX;
    centerY=cY;
}

public Robot(Bitmap bitmap, Point center) {
    this(bitmap, center.x, center.y);
}

public void setCenter(Point centerPoint) {
    centerX=centerPoint.x;
    centerY=centerPoint.y;
}

}

0 个答案:

没有答案