Problems with onKeyDown after resume

by Brian Durney » Wed, 17 Dec 2008 02:34:43 GMT

Sponsored Links
I have written a simple application that draws an image (Drawable) on
the screen.  When the user presses a key, the image is supposed to
move.  When I first start the application from Eclipse, the keys work
fine.  However, if I go to another screen in the emulator and then go
back to my application (with a long press of the Home button) then the
keys don't work any more.  The same problem occurs if I go to the
applications menu and click on the icon for my application--the key
events don't work.

I have included the code for my view below.  The only other file in
the application is an Activity that has a very simple onCreate method
to create an instance of this view and call setContentView().
Brian Durney

public class DrawView extends View {
        Bitmap img;
        Drawable botDraw;
        float x = 35, y = 100;

        public DrawView(Context context) {
        setFocusable(true);  // MIGHT BE NECESSARY FOR KEY EVENTS

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        img = BitmapFactory.decodeResource(context.getResources(),
        botDraw =  context.getResources().getDrawable

        public boolean onKeyDown(int keyCode, KeyEvent msg) {
                switch (keyCode) {
                case KeyEvent.KEYCODE_W:
                        y -= 5;
                        return true;
                case KeyEvent.KEYCODE_S:
                        y += 5;
                        return true;
                        return super.onKeyDown(keyCode, msg);

        public void onDraw(Canvas canvas) {
                Paint paint = new Paint();
                canvas.drawCircle(35.0f, 50.0f, 20.0f, paint);
                canvas.drawBitmap(img, x, y, null);
                canvas.rotate(45, 180, 80);  // ANGLE IN DEGREES, PIVOT X, 
                botDraw.setBounds(150, 50, 210, 110);


Other Threads

1. Failed to execute the Launcher built by myself

I need to customize the launcher application.
I built it from the android source, and installed it to the emulator
successfully(By renaming its package name to solve signing conflicts).
When I tried to run my build, an exception occurred, and I can't find
where it actually from because it seems to be from an internal thread.

After an whole day trying, I found that the exception is thrown by the
line in possibly:

Does anyone have any suggestion?
Thank you very much.


2. Webview load https URL always prompt error

In file
Code report NullPointerException in line
synchronized (mDefaultTrustManager) {

        // first, we validate the chain using the standard validation
        // solution; if we do not find any errors, we are done; if we
        // fail the standard validation, we re-validate again below,
        // this time trying to retrieve any individual errors we can
        // report back to the user.
        try {
            synchronized (mDefaultTrustManager) {
                    serverCertificates, "RSA");

                // no errors!!!
                return null;
        } catch (CertificateExcep


3. Free developer accounts for GPL / CC-BY* / etc developers

4. droid fonts

5. problem with porting mediaplayer to real board

6. Old changelogs?

7. keep printing android_power:wakeup and the machine can't boot