CalledFromWrongThreadException

 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

  • Setting span size of single row in StaggeredGridLayoutManager
  • Android - Listener for catching changes in a View's properties (e.g. android:layout_marginTop)
  • Android Studio Git Push Rejected
  • android 9 patch drawable xml
  • Android: Passing parameters into Activity from AndroidManifest.xml
  • Connection pool has been unable to grant a connection to thread
  • Related posts:

    Is there already a StopWatch class for android and why doesn't my implementation work?
    Manifest merger failed when trying to add my own logo to android app - Android Studio
    Failed to apply patch while updating android studio
    Cannot be cast to android.app.Fragment
    How To Test If Cursor Is Empty in a SQLiteDatabase Query
    How can I detect if the volume is set to mute?
  • Android Navigation Drawer (calling activities) with AbstractMainActivity
  • WebView redraw problem when loading data
  • Difference between extending LifecycleActivity,Activity,ActionbarActivity & AppCompactActivity?
  • Disable Click Event on Android ListView Items
  • Android: wrap_content is not working with ListView
  • How do I change the color of actionbar's up arrow?
  • 2 Solutions collect form web for “CalledFromWrongThreadException”

    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.

    I edited your 2nd function code, I see your code is loop forever cause the firstMethod call secondMethod and the secondMethod call the new firstMethod to start and then loop forever. I removed it and moved the code update ImageView into the UI Thread, could you try this:

    class ThraedDemo {
    
        private void firstMethod() {
            Thread objThread = new Thread() {
                @Override
                public void run() {
                    try {
                        runOnUiThread(new Runnable() {
                            public void run(){ 
                                ((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();
                }
            };
            objThread.start();
        }
    
        private void secondMethod() {
            Thread objThread2 = new Thread() {
    
                @Override
                public void run() {
                    try {
                        runOnUiThread(new Runnable() {
                            public void run(){ 
                                ((ImageView)findViewById(R.id.ImageViewnumber)).setImageResource(nums[n+1]);  
                            }
                        });
                        n++;
                        Thread.sleep(10000);
                        Log.v("Thread","22222222222 sleep");
                    } catch (InterruptedException ex) {
                        System.out.println("interuped exception" + ex.getMessage());
                    }
                }
            };
            objThread2.start();
        }
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.