麻烦高手指点一下!!!!
我现在在自己做一个android手机游戏,出了点问题,无法解决,请高手帮忙看一下,谢谢!!!!!
//*****************************************************************
//activity类
package wyf.wpf;
import android.app.Activity;//引入相关类
import android.os.Bundle;//引入相关类
import android.os.Handler;//引入相关类
import android.os.Looper;//引入相关类
import android.os.Message;//引入相关类
import android.view.KeyEvent;//引入相关类
import android.view.Window;//引入相关类
import android.view.WindowManager;//引入相关类
public class CrazyRabbitActivity extends Activity{
int action = 0;//键盘的状态,二进制表示 从左往右表示上下左右
boolean isSound = true;//是否播放声音
WelcomeView welcomeView;//WelcomeView的引用
FailView failview;//游戏失败引用
AboutView aboutView;//AboutView的引用
WinView winView;//欢迎界面的引用
ProcessView processView;//进度条界面的引用
//boolean isSound=true;
Handler myHandler = new Handler(){//用来更新UI线程中的控件
public void handleMessage(Message msg) {
if(msg.what == 1){
}
else if(msg.what == 2){
}
else if(msg.what == 3){//WelcomeView发来的消息,切换到AboutView
initAboutView();//切换到AboutView界面
}
else if(msg.what == 4){
if(aboutView != null){
aboutView = null;
}
toWelcomeView();//切换到WelcomeView界面
}
else if(msg.what == 7){
if(welcomeView != null){//释放欢迎界面
welcomeView = null;
}
if(processView != null){//释放加载界面
processView = null;
}
processView = new ProcessView(CrazyRabbitActivity.this,1);//初始化进度条并切换到进度条View
CrazyRabbitActivity.this.setContentView(processView);
new Thread(){//线程
public void run(){//重写的run方法
Looper.prepare();
welcomeView = new WelcomeView(CrazyRabbitActivity.this);//初始化WelcomeView
Looper.loop();
}
}.start();//启动线程
}
}
};
public void onCreate(Bundle savedInstanceState) {//创建是被创建
super.onCreate(savedInstanceState);
//全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
processView = new ProcessView(this,1);//初始化进度条并切换到进度条View
this.setContentView(processView);//设置加载界面
new Thread(){//线程
public void run(){
Looper.prepare();
welcomeView = new WelcomeView(CrazyRabbitActivity.this);//初始化WelcomeView
}
}.start();//启动线程
}
public void toWelcomeView(){//切换到欢迎界面
this.setContentView(welcomeView);
}
public void initAboutView(){//初始关于界面
aboutView = new AboutView(this);
this.setContentView(aboutView);
}
public boolean onKeyUp(int keyCode, KeyEvent event) {//键盘抬起
if(keyCode == 19){//上
action = action & 0x1F;
}
if(keyCode == 20){//下
action = action & 0x2F;
}
if(keyCode == 21){//左
action = action & 0x37;
}
if(keyCode == 22){//右
action = action & 0x3B;
}
if(keyCode == KeyEvent.KEYCODE_A){//A
action = action & 0x3D;
}
if(keyCode == 100){//B 留着备用
action = action & 0x3E;
}
return false;
}
public boolean onKeyDown(int keyCode, KeyEvent event){//键盘按下监听
if(keyCode == 19){//上
action = action | 0x20;
}
if(keyCode == 20){//下
action = action | 0x10;
}
if(keyCode == 21){//左
action = action | 0x08;
}
if(keyCode == 22){//右
action = action | 0x04;
}
if(keyCode == KeyEvent.KEYCODE_A){//A
action = action | 0x02;
}
if(keyCode == 100){//B留着备用
action = action | 0x01;
}
return false;
}
}
//********************************************************************
//AboutView
package wyf.wpf;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Message;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class AboutView extends SurfaceView implements SurfaceHolder.Callback {
CrazyRabbitActivity activity;
private TutorialThread thread;//刷帧的线程
Paint paint;
Bitmap background;
Bitmap about2;
///Bitmap about3;
//Bitmap about4;
Bitmap ok;
public AboutView(CrazyRabbitActivity activity) {//构造器
super(activity);
this.activity = activity;
getHolder().addCallback(this);
this.thread = new TutorialThread(getHolder(), this);
initBitmap();//初始化图片资源
}
public void initBitmap(){//初始化图片资源的方法
paint = new Paint();
background = BitmapFactory.decodeResource(getResources(), R.drawable.back_ground);
about2 = BitmapFactory.decodeResource(getResources(), R.drawable.about_2);
///about3 = BitmapFactory.decodeResource(getResources(), R.drawable.about_3);
//about4 = BitmapFactory.decodeResource(getResources(), R.drawable.about_4);
ok = BitmapFactory.decodeResource(getResources(), R.drawable.o_k);
}
public void onDraw(Canvas canvas){//自己写的绘制方法
//画的内容是z轴的,后画的会覆盖前面画的
canvas.drawColor(Color.WHITE);//将屏幕刷成白色
canvas.drawBitmap(background, 0,0, paint);
canvas.drawBitmap(about2, 0, 0, paint);
///canvas.drawBitmap(about3, 0, 0, paint);
//canvas.drawBitmap(about4, 60, 70, paint);
//canvas.drawRect(0, 0, 480, 48, paint);//绘制上下的黑框
//canvas.drawRect(0, 270, 480, 320, paint);
canvas.drawBitmap(ok, 250, 300, paint);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
this.thread.setFlag(true);
this.thread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
thread.setFlag(false);
while (retry) {
try {
thread.join();
retry = false;
}
catch (InterruptedException e) {//不断地循环,直到刷帧线程结束
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {//屏幕监听
if(event.getAction() == MotionEvent.ACTION_DOWN){
if(event.getX()>250 && event.getX()<250+ok.getWidth()
&& event.getY()>300 && event.getY()<300+ok.getHeight()){//点击了确定按钮
Message msg1 = activity.myHandler.obtainMessage(7);
activity.myHandler.sendMessage(msg1);
}
}
return super.onTouchEvent(event);
}
class TutorialThread extends Thread{//刷帧线程
private int span = 100;//睡眠的 毫秒数
private SurfaceHolder surfaceHolder;
private AboutView aboutView;
private boolean flag = false;
public TutorialThread(SurfaceHolder surfaceHolder, AboutView aboutView) {//构造器
this.surfaceHolder = surfaceHolder;
this.aboutView = aboutView;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public void run() {
Canvas c;
while (this.flag) {
c = null;
try {
// 锁定整个画布,在内存要求比较高的情况下,建议参数不要为null
c = this.surfaceHolder.lockCanvas(null);
synchronized (this.surfaceHolder) {
aboutView.onDraw(c);
}
} finally {
if (c != null) {
//更新屏幕显示内容
this.surfaceHolder.unlockCanvasAndPost(c);
}
}
try{
Thread.sleep(span);
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
}
//***********************************************************************************
//*******************************************************************************************8
//*************************************************************************************
以上是主要代码,可以再模拟器上运行,但是只要一运行AboutView,就会出现
“应用程序在(wyf.wpf进程中)意外停止,请重试,强制关闭”