IllegalArgumentException:UnknownBitmapConfiguration是什么导致我的游戏崩溃?

时间:2014-11-29 23:18:15

标签: java android exception

我正在制作“Android游戏编程傻瓜”一书,我得到了例外 IllegalArgumentException: UnknownBitmapConfiguration。我一直在寻找可能造成这种情况但却没有运气的地方。我对编程很陌生,所以任何帮助都会受到赞赏。

这是我的WhackAMoleView.java

        public WhackAMoleView(Context context, AttributeSet attrs){
            super(context,attrs);

            SurfaceHolder holder = getHolder();
            holder.addCallback(this);

            thread = new WhackAMoleThread(holder,context,
                new Handler(){
                @Override
                public void handleMessage(Message m){

                }
            });

            setFocusable(true);
        }
        public WhackAMoleThread getThread(){
            return thread;
        }

        class WhackAMoleThread extends Thread{

            public WhackAMoleThread(SurfaceHolder
                surfaceHolder, Context context, 
                    Handler handler){
                mySurfaceHolder = surfaceHolder;
                myContext = context;
                backgroundImg = 
                        BitmapFactory.decodeResource
                        (context.getResources(), R.drawable.title);
            }

            @Override 
            public void run(){
                while (running){
                    Canvas c = null;
                    try {
                        c = mySurfaceHolder.lockCanvas(null);
                        synchronized (mySurfaceHolder){
                            draw(c);
                        }
                    }finally{
                        if(c != null){
                            mySurfaceHolder.unlockCanvasAndPost(c);
                        }
                    }
                }

        }
        private void draw (Canvas canvas){
            try{
            canvas.drawBitmap(backgroundImg,0,0,null);
            if (!onTitle){
                canvas.drawBitmap(mole, mole1x, mole1y,null);
                canvas.drawBitmap(mole, mole2x, mole2y, null);
                canvas.drawBitmap(mole, mole3x, mole3y, null);
                canvas.drawBitmap(mole, mole4x, mole4y, null);
                canvas.drawBitmap(mole, mole5x, mole5y, null);
                canvas.drawBitmap(mole, mole6x, mole6y, null);
                canvas.drawBitmap(mole, mole7x, mole7y, null);
                canvas.drawBitmap(mask, (int) 50* drawScaleW,
                        (int) 450* drawScaleH, null);
                canvas.drawBitmap(mask, (int) 150* drawScaleW,
                        (int) 400* drawScaleH,null);
                canvas.drawBitmap(mask, (int) 250* drawScaleW,
                        (int) 450* drawScaleH,null);
                canvas.drawBitmap(mask, (int) 350* drawScaleW,
                        (int) 400* drawScaleH, null);
                canvas.drawBitmap(mask, (int) 450* drawScaleW,                              
                        (int) 450* drawScaleH, null);
                canvas.drawBitmap(mask, (int) 550* drawScaleW,
                        (int) 400* drawScaleH,null);
                canvas.drawBitmap(mask, (int) 650* drawScaleW,
                        (int) 450* drawScaleH, null);
            }
            }catch (Exception e){
            }
        }

        boolean doTouchEvent(MotionEvent event){
            synchronized(mySurfaceHolder){
                int eventaction = event.getAction();
                int X = (int)event.getX();
                int Y = (int)event.getY();

                switch(eventaction){

                case MotionEvent.ACTION_DOWN:
                    break;

                case MotionEvent.ACTION_MOVE:
                    break;

                case MotionEvent.ACTION_UP:
                    if(onTitle){
                        backgroundImg=
                            BitmapFactory.decodeResource
                            (myContext.getResources(),
                            R.drawable.background);
                        backgroundImg = 
                            Bitmap.createScaledBitmap
                            (backgroundImg, screenW, screenH, true);
                        mask = BitmapFactory.decodeResource(myContext.getResources(),
                                R.drawable.mask);
                        mole = BitmapFactory.decodeResource(myContext.getResources(),
                                R.drawable.mole);
                        scaleW = (float) screenW/ (float)
                                backgroundOriginW;
                        scaleH = (float) screenH/ (float)
                                backgroundOriginH;
                        mask = Bitmap.createScaledBitmap(mask, (int)(mask.getWidth()*scaleW), (int)(mask.getHeight()*scaleH), true);
                        mole = Bitmap.createScaledBitmap(mole, (int)(mole.getWidth()*scaleW), (int)(mole.getHeight()*scaleH), true);
                        onTitle = false;
                        pickActiveMole();
                    }

                    break;

                }
        }
            return true;
        }
        public void setSurfaceSize(int width,
                int height){
            synchronized (mySurfaceHolder){
                screenW = width;
                screenH = height;
                backgroundImg = Bitmap.createScaledBitmap(
                        backgroundImg, width,
                        height, true);
                drawScaleW = (float) screenW / 800;
                drawScaleH = (float) screenH / 600;
                mole1x = (int) (55* drawScaleW);
                mole2x = (int) (155* drawScaleW);
                mole3x = (int) (255* drawScaleW);
                mole4x = (int) (355* drawScaleW);
                mole5x = (int) (455* drawScaleW);
                mole6x = (int) (555* drawScaleW);
                mole7x = (int) (655* drawScaleW);
                mole1y = (int) (475* drawScaleH);
                mole2y = (int) (425* drawScaleH);
                mole3y = (int) (475* drawScaleH);
                mole4y = (int) (425* drawScaleH);
                mole5y = (int) (475* drawScaleH);
                mole6y = (int) (425* drawScaleH);
                mole7y = (int) (475* drawScaleH);
            }
        }
        public void setRunning(boolean b){
            running = b;
        }
        private void animateMoles(){
            if(activeMole == 1){
                if(moleRising){
                    mole1y -= moleRate;
                }else if (moleSinking){
                    mole1y += moleRate;
                }
                if(mole1y >= (int) (475* drawScaleH) || moleJustHit){
                    mole1y = (int) (475* drawScaleH);
                    pickActiveMole();
                }
                if(mole1y <= (int) (300* drawScaleH)){
                    mole1y = (int) (300* drawScaleH);
                    moleRising = false;
                    moleSinking = true;
                }
            }
            if(activeMole == 2){
                if(moleRising){
                    mole2y -= moleRate;
                }else if (moleSinking){
                    mole2y += moleRate;
                }
                if(mole2y >= (int) (425* drawScaleH) || moleJustHit){
                    mole2y = (int) (425* drawScaleH);
                    pickActiveMole();
                }
                if(mole2y <= (int) (250* drawScaleH)){
                    mole2y = (int) (250* drawScaleH);
                    moleRising = false;
                    moleSinking = true;
                }
            }
            if(activeMole == 3){
                if(moleRising){
                    mole3y -= moleRate;
                }else if (moleSinking){
                    mole3y += moleRate;
                }
                if(mole3y >= (int) (475* drawScaleH) || moleJustHit){
                    mole3y = (int) (475* drawScaleH);
                    pickActiveMole();
                }
                if(mole3y <= (int) (300* drawScaleH)){
                    mole3y = (int) (300* drawScaleH);
                    moleRising = false;
                    moleSinking = true;
                }
            }
            if(activeMole == 4){
                if(moleRising){
                    mole4y -= moleRate;
                }else if (moleSinking){
                    mole4y += moleRate;
                }
                if(mole4y >= (int) (425* drawScaleH) || moleJustHit){
                    mole4y = (int) (425* drawScaleH);
                    pickActiveMole();
                }
                if(mole4y <= (int) (250* drawScaleH)){
                    mole4y = (int) (250* drawScaleH);
                    moleRising = false;
                    moleSinking = true;
                }
            }
            if(activeMole == 5){
                if(moleRising){
                    mole5y -= moleRate;
                }else if (moleSinking){
                    mole5y += moleRate;
                }
                if(mole5y >= (int) (475* drawScaleH) || moleJustHit){
                    mole5y = (int) (475* drawScaleH);
                    pickActiveMole();
                }
                if(mole5y <= (int) (300* drawScaleH)){
                    mole5y = (int) (300* drawScaleH);
                    moleRising = false;
                    moleSinking = true;
                }
            }
            if(activeMole == 6){
                if(moleRising){
                    mole6y -= moleRate;
                }else if (moleSinking){
                    mole6y += moleRate;
                }
                if(mole6y >= (int) (425* drawScaleH) || moleJustHit){
                    mole6y = (int) (425* drawScaleH);
                    pickActiveMole();
                }
                if(mole6y <= (int) (250* drawScaleH)){
                    mole6y = (int) (250* drawScaleH);
                    moleRising = false;
                    moleSinking = true;
                }
            }
            if(activeMole == 7){
                if(moleRising){
                    mole7y -= moleRate;
                }else if (moleSinking){
                    mole7y += moleRate;
                }
                if(mole7y >= (int) (475* drawScaleH) || moleJustHit){
                    mole7y = (int) (475* drawScaleH);
                    pickActiveMole();
                }
                if(mole7y <= (int) (300* drawScaleH)){
                    mole7y = (int) (300* drawScaleH);
                    moleRising = false;
                    moleSinking = true;
                }
            }
        }
        private void pickActiveMole(){
            activeMole = new Random().nextInt(7) + 1;
            moleRising = true;
            moleSinking = false;
        }

    }
    @Override
    public boolean onTouchEvent(MotionEvent event){
    return thread.doTouchEvent(event);
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int
        format, int width, int height){
    thread.setSurfaceSize(width, height);
    }

@Override public void surfaceCreated(SurfaceHolder holder){
    thread.setRunning(true);
    if(thread.getState() == Thread.State.NEW){
        thread.start();
    }
}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
    thread.setRunning(false);
}
}

我的WhackAMoleActivity

    public class WhackAMoleActivity extends Activity{

    private static final int TOGGLE_SOUND = 1;
    private boolean soundEnabled = true;

    private WhackAMoleView myWhackAMoleView;

    /** Called when the activity is first created. */
    @Override
    public 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.whackamole_layout);
        myWhackAMoleView = (WhackAMoleView)
            findViewById(R.id.mole);
        myWhackAMoleView.setKeepScreenOn(true);
    }
    public  boolean onCreateOptionsMenu(Menu menu){
        MenuItem toggleSound = menu.add(0, TOGGLE_SOUND,
            0,"Toggle Sound");
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
        case TOGGLE_SOUND:
            String soundEnabledText = "Sound On";
            if(soundEnabled){
                soundEnabled=false;
                soundEnabledText="Sound Off";
            }else{
                soundEnabled = true;
            }
            Toast.makeText(this, soundEnabledText, Toast.LENGTH_SHORT).show();
            break;
        }
        return false;
    }
}

错误消息......

    11-29 17:58:51.008: E/InputEventReceiver(21188): Exception dispatching input event.
11-29 17:58:51.008: E/MessageQueue-JNI(21188): Exception in MessageQueue callback: handleReceiveCallback
11-29 17:58:51.048: E/MessageQueue-JNI(21188): java.lang.IllegalArgumentException: unknown bitmap configuration
11-29 17:58:51.048: E/MessageQueue-JNI(21188):  at android.graphics.Bitmap.nativeCreate(Native Method)
11-29 17:58:51.048: E/MessageQueue-JNI(21188):  at android.graphics.Bitmap.createBitmap(Bitmap.java:929)
11-29 17:58:51.048: E/MessageQueue-JNI(21188):  at android.graphics.Bitmap.createBitmap(Bitmap.java:902)
11-29 17:58:51.048: E/MessageQueue-JNI(21188):  at android.graphics.Bitmap.createBitmap(Bitmap.java:834)
11-29 17:58:51.048: E/MessageQueue-JNI(21188):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:710)
11-29 17:58:51.048: E/MessageQueue-JNI(21188):  at com.agpfd.whackamole.WhackAMoleView$WhackAMoleThread.doTouchEvent(WhackAMoleView.java:151)

任何帮助将不胜感激,谢谢:)

1 个答案:

答案 0 :(得分:0)

嘿,我刚想通了...... 在WhackAMoleThread构造函数中,我从未声明

的值
  BackgroundOriginW

  BackgroundOriginH

所以当我尝试执行这些行

scaleW = (float) screenW/ (float)
                            backgroundOriginW;

scaleH = (float) screenH/ (float)
                            backgroundOriginH;

他们从未收到

所需的价值
mask = Bitmap.createScaledBitmap(mask, (int)(mask.getWidth()*scaleW), (int)(mask.getHeight()*scaleH), true); 
mole = Bitmap.createScaledBitmap(mole, (int)(mole.getWidth()*scaleW), (int)(mole.getHeight()*scaleH), true); 

也是我的错,我没有发布我的变量声明给你们看,所以你们更难以帮助......

我很感激,但谢谢你们的帮助!!!