tags:

views:

47

answers:

1
enter code here public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final ThraedDemo objDemo = new ThraedDemo();
    Thread objThread = new Thread() {

        @Override
        public void run() {

            objDemo.firstMethod();
        }
    };
    objThread.start();
}

class ThraedDemo {

    private void firstMethod() {
        Thread objThread = new Thread() {

            @Override
            public void run() {
                try {
                ((ImageView)findViewById(R.id.ImageViewnumber)).setImageResource(nums[n]);

                    Thread.sleep(10000);
                    Log.v("Thread","1111111111111111sleep");
                } catch (InterruptedException ex) {
                    System.out.println("interuped exception" + ex.getMessage());
                }
                secondMethod();
            }

            private void secondMethod() {
    Thread objThread = new Thread() {

        @Override
        public void run() {
            try {
            ((ImageView)findViewById(R.id.ImageViewResult)).setImageResource(nums[n+1]);

            n++;

                Thread.sleep(10000);
               Log.v("Thread","22222222222 sleep");
            } catch (InterruptedException ex) {
                System.out.println("interuped exception" + ex.getMessage());
            }
            firstMethod();
        }
    };
    objThread.start();
}

        };
        objThread.start();
    }

}

I use the above code but it is not running.it got CalledFromWrongThreadException what is the problem inb the above code.Please give me some suggestions.Thanks in advance

+1  A: 

I think you can't do view modifications from another thread than the UI thread, so either create handlers in the oncreate and post your thread to it, or use AsyncTask, or runOnUIThread method to send portions of code directly to the UI thread.

Sephy
If you want your 2 threads to be executed one after the other, why not simply post your 2 threads to the same handler?
Sephy
"Related to your question" - what is your question then? I thought that Sephy gave an appropriate answer to your only question in text. Be more specific.
LordTwaroog
Thank u for your response Sephy, and how we use the 2 threads in handlers.Give me some sample code.
sairam