jersam515
jersam515

Reputation: 657

Why is my thread stopping due to an uncaught exception?

I have the following thread:

class LemonadeMainMenuThread extends Thread {

    private SurfaceHolder mSurfaceHolder;
    private Handler mHandler;
    private Context mContext;
    int mRowId = 0;

    public void setRunning(boolean b) {
        mRun = b;
    }

    public LemonadeMainMenuThread(SurfaceHolder surfaceHolder,
            Context context, Handler handler) {
        mSurfaceHolder = surfaceHolder;
        mHandler = handler;
        mContext = context;
    }

    Resources res = getResources();

    @Override
    public void run() {
        while(1==1)
        {
        if (mRun) {
            try {
                c = mSurfaceHolder.lockCanvas(null);
                synchronized (mSurfaceHolder) {
                    doDraw(c);

                }
            } finally {
                if (c != null) {
                    mSurfaceHolder.unlockCanvasAndPost(c);
                }
            }

        }
        else{try { Thread.sleep(100); } catch (Exception ie) {}}
    }}

}

I also have the following methods in the same file:

public void surfaceCreated(SurfaceHolder arg0) {
    if (thread.getState() == Thread.State.TERMINATED) {
        thread = new LemonadeMainMenuThread(holder, ctx, new Handler() {
            @Override
            public void handleMessage(Message m) {
            }
        });
    }
    thread.setRunning(true);
    thread.start();
}
public LemonadeMainMenuThread setThreadRunning(boolean b) {
    mRun = b;
    if (thread.getState() == Thread.State.TERMINATED) {
        thread = new LemonadeMainMenuThread(holder, ctx, new Handler() {
            @Override
            public void handleMessage(Message m) {
            }
        });
    }
    return thread;
}

On my logic file, I have OnPause and OnResume events as follows:

@Override
protected void onPause() {
    mLemonadeMainMenuView.setThreadRunning(false);
    super.onPause();
}

@Override
protected void onResume() {
    mLemonadeMainMenuView.setThreadRunning(true);
    super.onResume();
    b=true;
}

The thread works fine normally, however when I exit the app and come back to it, it closes with the following errors:

FATAL EXCEPTION: Thread-13
03-27 
java.lang.NullPointerException
03-27
at com.example.HelloAndroid.LemonadeMainMenuView$LemonadeMainMenuThread.doDraw(LemonadeMainMenuView.java:106)

at com.example.HelloAndroid.LemonadeMainMenuView$LemonadeMainMenuThread.run(LemonadeMainMenuView.java:72)

Here is the DoDraw() method:

    private void doDraw(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
        Paint p = new Paint();
        p.setColor(Color.RED);
        p.setStyle(Style.FILL);
        Paint p3 = new Paint();
        p3.setColor(Color.GREEN);
        p3.setStyle(Style.FILL);
        Paint p2 = new Paint();
        p2.setColor(Color.MAGENTA);
        p2.setStyle(Style.FILL);
        Paint paint = new Paint();
        paint.setColor(Color.TRANSPARENT);
        paint.setStyle(Style.FILL);

        canvas.drawBitmap(mFrog, mx, my, null);
        canvas.drawRect(0, height * 4 / 5, width, height, p);
        int fly=0;
        while(fly<5)
        {
        fly++;
        int xer=flyserd[(fly-1)*2];
        int yer=flyserd[((fly-1)*2)+1];
        canvas.drawCircle(xer, yer, 10, p3);
        }
        fly=0;
        if (clicked) {
            if (stages == 0) {
                stages = (int) (Math.sqrt(Math.pow(MouthX + mx + x + clickx
                        + x / 2, 2)
                        + Math.pow(MouthY + my + y + clicky + y / 2, 2))
                        * height / 40000);
                stages += 1;
            }

            if (systemtime + 10 < System.currentTimeMillis()) {
                systemtime = System.currentTimeMillis();
                if (stage < stages) {
                    stage++;
                    canvas.drawLine(MouthX + mx, MouthY + my,
                            ((clickx - (MouthX + mx)) * stage / stages)
                                    + MouthX + mx,
                            ((clicky - (MouthY + my)) * stage / stages)
                                    + MouthY + my, p2);
                    if (stage == stages) {
                        stage = 0;
                        stages = 0;
                        clicked = false;
                    }
                }
            }
        }
        b = false;
    }

Upvotes: 1

Views: 218

Answers (2)

duffymo
duffymo

Reputation: 308743

Follow the stack trace - it tells you the file, the class, the line number where the NPE occurred. Turn on line numbers in your IDE, find that line, and see which of the references you find is nul. Step through in a debugger. It should be easy to find - faster than asking here.

Upvotes: 2

Win Myo Htet
Win Myo Htet

Reputation: 5457

I think that your canvas get destroyed along with the activity when you press back button

try this

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
} 

Upvotes: 1

Related Questions