Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8592965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:52:42+00:00 2026-06-11T23:52:42+00:00

I have two classes: An activity class that controls several widgets and then a

  • 0

I have two classes: An activity class that controls several widgets and then a custom SurfaceView class that implements a Thread. (Fairly common). I have implemented a simple custom listener in the SurfaceView class that I can trigger whenever certain values change in the thread that I want to update in various widgets in the Activity class.

public class MyActivity extends Activity{

    CustomView myThreadView = (CustomView)findViewById(...);

    myThreadView.setListener(new OnStatChangeListener(){
        public void onChange(int change){
            //Change various widgets based on value fed in.
        }
    });

    // All of the other things (View setups, listeners, onCreate, etc.)
}

The Thread class:

public class CustomView extends SurfaceView implements Runnable(){
    //.. The usual thread and View stuff.

    public sListener;

    public interface OnStatChangeListener {
        public onChange(int change);
    }

    public void setListener(OnStatChangeListener oscl){
        sListener = oscl;
    }

    public void externalAccessMethod(){
        // Some thing changes a stat that is reflected in the Activity's widgets.
        sListener.onChange(value);
    }

    public void methodRunByThread(){
        // Something else changes that I want to update.
        sListener.onChange(value);
    }

    // Everything else (not relevant).
}

Whenever I call the onChange from a method that was called by the Activity’s class, it works without issue. However, whenever the thread itself updates it’s state (in my case a redraw method) and I call the onChange method, I get a CalledFromWrongThread exception. Can someone explain why one works but not the other, and what the best practice is for actually implementing a widget update from a separate thread? A separate handler thread in the Activity does not seem necessary, since I know exactly when I am updating the value anyway.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:52:43+00:00Added an answer on June 11, 2026 at 11:52 pm

    Because you only can access views from the UI thread. There’s a single UI thread per application. Now you access the views from another thread which is not correct and results inh exception.

    You can do either this:

    in your Activity’s onChange wrap everything in runOnUiThread

    or better:

    use Handlers instead of your own listeners (needs some learning but pays)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes that extend the activity class. Each class has it's own
I have two classes Foo and Bar that Bar extends Foo as below: class
I have two classes, one that inherits from the other. The base class is
I have two classes(Book,Software) that inherit from the Product class. these two classes have
I have two classes that don't know anything about themselfs class A, class B.
I have two classes: class Activity < ActiveRecord::Base belongs_to :activity_type def belongs_to_cat_a? self.activity_category ==
Actually I have two java classes of which one is an activity class. Now
I have two classes A and B A is main activity class and B
I have two classes which extend Activity and need to call another class method
I have two classes in question. Both extend Activity. Class A public void displayinfo()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.