Click here to Skip to main content
11,927,006 members (52,297 online)
Rate this:
Please Sign up or sign in to vote.
See more: Java Mobile Android
I wrote a lot of code for graphical remote control via mobile, my app uses SurfaceView, canvas and sockets, but I got 2 errors:
1)Application crashing when I click Home button.
2)Application crashing when I change device orientation.
Could you point me right direction? Please help.

EDIT: second problem solvedSmile | :)

package com.example;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.ArrayList;
public class MyActivity extends Activity {
     * Called when the activity is first created.
    public boolean _run = false;
    Panel pnl;
    public void onCreate(Bundle savedInstanceState) {
        pnl = new Panel(this);
    class Panel extends SurfaceView implements SurfaceHolder.Callback {
        private TutorialThread _thread;
        public Panel(Context context) {
            _thread = new TutorialThread(getHolder(), this);
        public boolean onTouchEvent(MotionEvent event) {
            synchronized (_thread.getSurfaceHolder()) {
                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                return true;
        public void onDraw(Canvas canvas) {
            Paint mPaint = new Paint();
            //int color = 0xaf000000;
            canvas.drawText("something", 50, 50, mPaint);
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            // TODO Auto-generated method stub
        public void surfaceCreated(SurfaceHolder holder) {
        public void surfaceDestroyed(SurfaceHolder holder) {
            // simply copied from sample application LunarLander:
            // we have to tell thread to shut down & wait for it to finish, or else
            // it might touch the Surface after we return and explode
            boolean retry = true;
            while (retry) {
                try {
                    retry = false;
                } catch (InterruptedException e) {
                    // we will try it again and again...
    class TutorialThread extends Thread {
        private SurfaceHolder _surfaceHolder;
        private Panel _panel;
        //public boolean _run = false;

        public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
            _surfaceHolder = surfaceHolder;
            _panel = panel;
        public void setRunning(boolean run) {
            _run = run;
        public SurfaceHolder getSurfaceHolder() {
            return _surfaceHolder;
        public void run() {
            Canvas c;
            while (_run) {
                }catch(Exception x){}
                c = null;
                try {
                    //if(drawn == false)
                        //]drawn = true;
                        c = _surfaceHolder.lockCanvas(null);
                        synchronized (_surfaceHolder) {
                        }catch (Exception cc)
                } finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                //catch (Exception cx)
                  //      {}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    <uses-sdk android:minSdkVersion="8"/>
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="MyActivity"
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
From LogCat:
java.lang.IllegalThreadStateException: Thread already started.
How to fix? Please help!
Posted 12-Jan-13 8:31am
Edited 14-Jan-13 4:31am
Sandeep Mewara 12-Jan-13 15:09pm
How come anyone would even comment on it without seeing what code you have written around 'home' button.
halabella 12-Jan-13 15:27pm
Attached source.
ridoy 13-Jan-13 1:23am
Please show LogCat view here,for which reason you app crashing.
halabella 14-Jan-13 9:32am
Added LogCat results.
halabella 13-Jan-13 7:40am
How to set LogCat? I'm using Intellij IDEA.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

whats your manifest look like? do you mean home as in your main menu or home as in androids home screen? make sure your launcher is defined.
halabella 13-Jan-13 7:38am
Attached manifest if its matter. 'Home' in android emulator, not in my app menu.
analogx 4-Feb-13 9:50am
one more thing can you post your solution so that others can benefit from it...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Thanks to all, I solved it myself by inserting one line of code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100