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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:19:05+00:00 2026-06-08T07:19:05+00:00

How can I pass the context and the name string as arguments to the

  • 0

How can I pass the context and the name string as arguments to the new thread?

Errors in compilation:

Line

label = new TextView(this);

The constructor TextView(new Runnable(){}) is undefined

Line “label.setText(name);” :

Cannot refer to a non-final variable name inside an inner class defined in a different method

Code:

public void addObjectLabel (String name) {
    mLayout.post(new Runnable() {
        public void run() {
            TextView label;
            label = new TextView(this);
            label.setText(name);
            label.setWidth(label.getWidth()+100);
            label.setTextSize(20);
            label.setGravity(Gravity.BOTTOM);
            label.setBackgroundColor(Color.BLACK);
            panel.addView(label);
        }
    });
}
  • 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-08T07:19:07+00:00Added an answer on June 8, 2026 at 7:19 am

    You need to declare name as final, otherwise you can’t use it in an inner anonymous class.

    Additionally, you need to declare which this you want to use; as it stands, you’re using the Runnable object’s this reference. What you need is something like this:

    public class YourClassName extends Activity { // The name of your class would obviously be here; and I assume it's an Activity
        public void addObjectLabel(final String name) { // This is where we declare "name" to be final
            mLayout.post(new Runnable() {
                public void run() {
                    TextView label;
                    label = new TextView(YourClassName.this); // This is the name of your class above
                    label.setText(name);
                    label.setWidth(label.getWidth()+100);
                    label.setTextSize(20);
                    label.setGravity(Gravity.BOTTOM);
                    label.setBackgroundColor(Color.BLACK);
                    panel.addView(label);
                }
            });
        }
    }
    

    However, I’m not sure this is the best way to update the UI (you should probably be using runOnUiThread and AsyncTask). But the above should fix the errors you’ve encountered.

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

Sidebar

Related Questions

Can I pass value to Navigation Context like this: NavigationContext.QueryString[param1] = PARAM1; Is it
How can I traverse through this set of data? String[] from = new String[]{AppSQLite.KEY_NAME,AppSQLite.KEY_ICON};
According to this thread on the django-developers list, I can't pass the constant False
Can anyone tell me how I can correctly pass my application context to my
I know you can pass arguments through the RunWorkerAsync function call when you first
I know that I can pass a string as the second parameter to the
If I have a auto_ptr I can pass it for a reference?Like: auto_ptr<MyClass>Class(new MyClass);
Is there any way that I can pass arguments in selector? example: I have
You can pass special strings into jQuery's Datepicker class setDate() method like +7 which
in MongoDb I can pass a predicate to an queryable instance for example DataBase.GetCollection<BsonDocument>(entity)

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.