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 8521839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:56:20+00:00 2026-06-11T06:56:20+00:00

I have a Thread nested in the main activity: public class MainActivity extends Activity

  • 0

I have a Thread nested in the main activity:

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);

    new Thread(new Runnable(){
        public void run() {
            int myInt = 1;
            // Code below works fine and shows me myInt
            TextView textView = (TextView) findViewById(R.id.text_view);
            textView.setText(myInt);
        }
    }).start();

    // Code below doesn't work at all
    TextView textView = (TextView) findViewById(R.id.text_view);
    textView.setText(myInt);

}

I’m not sure if this structure is correct at all. How should I pass myInt variable to the MainActivity so it will become recognizable and operable outside the Thread?

  • 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-11T06:56:22+00:00Added an answer on June 11, 2026 at 6:56 am

    You would first want a global variable for the integer that is already set before you try setting the textview that’s outside of your thread (on the main thread). It needs to be set beforehand because the new thread you start will simply be started and move along to the next lines of code, so myInt won’t have been set yet.

    Then, use the predetermined global integer value for the textview on the main thread, at least initially. If you would like to change it from within the thread you started, then create a method in your class like setIntValue() which will pass in the integer from the thread and set the global variable to that value. I can update with code examples later if you’d like.

    UPDATE: example code

    public class MainActivity extends Activity {
    
    //your global int
    int myInt
    
    public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);
    
    new Thread(new Runnable(){
        public void run() {
            int myRunnableInt = 1;
            // Code below works fine and shows me myInt
            TextView textView = (TextView) findViewById(R.id.text_view);
            textView.setText(String.valueOf(myRunnableInt));
    
            //say you modified myRunnableInt and want the global int to reflect that...
            setMyInt(myRunnableInt);
        }
    }).start();
    
    //go ahead and initialize the global one here because you can't directly access your
    myRunnableInt
    myInt = 1;
    
    TextView textView = (TextView) findViewById(R.id.text_view);
    textView.setText(String.valueOf(myInt)); //now you will have a value here to use
    
    //method to set the global int value
    private void setMyInt(int value){
        myInt = value;
    
        //you could also reset the textview here with the new value if you'd like
        TextView textView = (TextView) findViewById(R.id.text_view);
        textView.setText(String.valueOf(myInt));
    }
    }
    

    Note: If you only wish to be able to reset the textview, as opposed to having a global, operable variable, I would suggest changing the method to just pass in the new integer and set the textview, rather than storing a global variable, like this:

    private void setTextView(int newInt){
        TextView textView = (TextView) findViewById(R.id.text_view);
        textView.setText(String.valueOf(newInt));
    }
    

    If you do the above, make sure when you call the method from within the thread, call it on the UI thread like so:
    runOnUiThread(new Runnable()){
    public void run(){
    //update UI elements
    }
    }

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

Sidebar

Related Questions

I have a nested inner class that extends AsyncTask to run a db query
I have a thread like this public class SMS { class Read implements Runnable
I have a main thread that makes some other threads in two nested for.
Suppose I have thread 1, the main window UI thread and thread 2, a
I have thread where downloading xml file a this xml file i want to
Is if almost always required to have thread syncing (i.e. use of mutex, semaphores,
I have a thread which needs to be run in the background every 1000ms.
I have a thread from a thread pool in a service that does quite
I have some thread to write resource and some to read it.But pthread_rwlock cause
I have several thread pools and I want my application to handle a cancel

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.