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

  • Home
  • SEARCH
  • 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 7995297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:22:11+00:00 2026-06-04T14:22:11+00:00

I have a problem with that code setContentView(R.layout.game); TextView text = (TextView) findViewById(R.id.qwe_string); text.setText(Hello

  • 0

I have a problem with that code

setContentView(R.layout.game);
TextView text = (TextView) findViewById(R.id.qwe_string);
text.setText("Hello Android");

It works if it’s inside an activity, but if it isn’t, obviously, it gives errors:

The method findViewById(int) is undefined for the type new TimerTask(){}
The method setContentView(int) is undefined for the type new TimerTask(){}

The code is inside a timer in separate class (not activity). The full code is as follows.

//main timer task for ingame time processing
static Timer gameTimer = new Timer();
static TimerTask gameTimerTask = new TimerTask() {
    @Override
    public void run() {
        setContentView(R.layout.game);
        TextView text = (TextView) findViewById(R.id.qwe_string);
        text.setText("Hello Android");
    }
};

I tried changing it like that

ScreenGame.this.setContentView(R.layout.game);
TextView text = (TextView) ScreenGame.this.findViewById(R.id.qwe_string);
text.setText("Hello Android");

But it still doesn’t work 🙁

PS – Yes, I searched other similar questions, but all of them although appear to be the same, in fact completely different.

  • 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-04T14:22:12+00:00Added an answer on June 4, 2026 at 2:22 pm
    ScreenGame.this.setContentView(R.layout.game);
    TextView text = (TextView) ScreenGame.this.findViewById(R.id.qwe_string);
    text.setText("Hello Android");
    

    is exactly the same as

    setContentView(R.layout.game);
    TextView text = (TextView) findViewById(R.id.qwe_string);
    text.setText("Hello Android");
    

    wrong code

    You should make a parent class, that fires off all the other Activities. In that parent class, you can have a reference to each child activity, and you can ask each child for it’s textfield:

    // In the parent:
    child.getTextView().setText("Hello galaxy!");
    
    // with the child method:
    TextView getTextView () {
      return (TextView) findViewById(R.id.qwe_string);
    }
    

    EDIT: More info:

    The code I gave was not good, I hadn’t understood your problem well… I hope I do now, do I’ll try to correct myself.

    Make a seperate class, for example MyTimer, which will extends the TimerTask class:

    class MyTimer extends TimerTask {
      // your class
    }
    

    Make a constructor, that excepts an TextView as an argument, and saves a reference to it.

    TextView theTextView;
    MyTimer (TextView tv) {
      this.theTextView = tv;
    }
    

    Now implement run():

    @Override
    public void run() {
        setContentView(R.layout.game);
        thetextView.setText("Hello Galaxy!");
    }
    

    Call make this class using the following code:

    static TimerTask gameTimerTask = new MyTimer((TextView) findViewById(R.id.qwe_string));
    

    I hope this is all correct, I have to do this from memory as I don’t have any testing environment. At least it should help you in the right direction.

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

Sidebar

Related Questions

I recently have a problem that my java code works perfectly ok on my
I have some code that I'd like to run on a page. My problem
Hi I have a code example available here: http://jsbin.com/oxoweh My problem is that I
I have a code problem which stems from the fact that I am using
I have a custom title bar requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activities); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); Which works basically fine.
I built my own TabHost with this code: I have a problem that when
I have a problem with a textview in android. I made programatically a tablelayout,
I have a problem with this code. This is a tutorial code that show
I have a problem with my Android app. I have three Activities that all
In a couple of scripts that I use I have problem that is intermittent.

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.