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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:13:10+00:00 2026-05-30T08:13:10+00:00

There are so many things wrong with the following code, but yet no errors

  • 0

There are so many things wrong with the following code, but yet no errors and shown/given.

  1. btn3.setText(“fud”); should throw an NullPointerException. It doesn’t.
  2. I should not be allowed to modify UI elements from a non UI thread. I can and Android doesn’t complain about it.

What is going on here?

public class TestApp1Activity extends Activity {

private static final String TAG = TestApp1Activity.class.getSimpleName();
private Button btn1, btn2, btn3;
private ExecutorService threadPool;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btn1 = (Button)findViewById(R.id.button1);
    btn2 = (Button)findViewById(R.id.button2);

    threadPool = Executors.newFixedThreadPool(4);
    threadPool.submit(new Runnable() {
        @Override
        public void run() {
            btn3.setText("fud"); // should give a NullPointerException. Doesn't only stops execution.
            btn2.setText("From pool"); // shouldn't be able to touch UI comps from non-UI thread
        }
    });

    new Thread(new Runnable() {
        @Override
        public void run() {
            Log.i(TAG, "Before setting text on btn");
            btn1.setText("From thread"); // shouldn't be able to touch UI comps from non-UI thread
            Log.i(TAG, "After setting text on btn");

                            // So below actually throws the error I wanted.
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.cox1);
            imageView.setImageBitmap(bm);
        }
    }).start();
}
}

EDIT: Since I’m getting bad answers, let me answer my own questions.

  1. The ExecutorService.submit wraps the Runnable in a try/catch. If any errors are thrown, the are quietly discarded. This is terribly design by the Android team…gezzz
  2. So apparently you can modify some view components from another thread. Just not all of them.
  • 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-05-30T08:13:11+00:00Added an answer on May 30, 2026 at 8:13 am

    1) The ExecutorService.submit wraps the Runnable in a try/catch. If any errors are thrown, the are quietly discarded. This is terribly design. Never should an error be quietly suppressed.

    2) So apparently you can modify some view components from another thread. Just not all of them. The component just sets it’s properties and on the next onDraw the component is repainted. You can actually do this with a lot of components, but the Android team says “you could get unexpected results.” So avoid it.

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

Sidebar

Related Questions

There are many things that all programmers should know, but I am particularly interested
Is there anything wrong with checking so many things in this unit test?: ActualModel
There are many examples on the internet of doing this type of thing. But
There are many ways of doing debugging, using a debugger is one, but the
There are many tools out there for writing and managing requirements, but are there
Yes, there are many topics about that, but I still didn't get it. I
In methods of controllers, there are usually many checks for doing the right thing.
Is there such a thing as too many stored procedures? I know there is
There are many ASP.MVC blog post bits and pieces scattered over different web sites,
There are many techniques to enforce strong passwords on website: Requesting that passwords pass

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.