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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:03:43+00:00 2026-06-07T10:03:43+00:00

I want to do some long operation (like copying/loading file) when the application is

  • 0

I want to do some long operation (like copying/loading file) when the application is created. I created a thread to do so, that thread DOES NOT update UI. I got an error saying cannot create handler in a thread without calling Looper.prepare(). What’s wrong with my code?

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

    threadFileManager = new Thread (
        new Runnable() {
            public void run() {
                FileManager fM = new FileManager();
                fM.copyFileFromAssetToStorage();                
        }
    }); 
    threadFileManager.start();
}

Edit:The error lied in my FileManager class, when it was a subclass of Activity. Changing to Service worked.

  • 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-07T10:03:44+00:00Added an answer on June 7, 2026 at 10:03 am

    The Application class is the wrong place to do this. If you want, it’s acceptable to use the Application‘s onCreate() method to start a Service. You should implement this background thread in a Service, as the purpose of Service is to do things in the background. The Application class should seldom be used. It’s a last resort for maintaining minimal amounts of global state.

    Once you move this code into a Service, Looper.prepare() will already have been called for you by Android.

    EDIT:

    OP was actually trying to create a Handler inside of the Thread when he invoked the constructor of his FileManager class. While my comment above is still true, it is not relevant to OP’s question as he was subclassing Activity and not Application.

    To be clear, the actual problem was that he was creating a Handler inside a Thread which had not yet called Looper.prepare() (via new FileManager()). The correct fix would be to create the Handler on the main thread, i.e. in one of the Activity or Service callbacks.

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

Sidebar

Related Questions

I want to overload the assignment operator for types like int, long etc. That
I want to make a long list short by hiding some elements in long
I want some of the goodies in a ListView, like being able to use
I want some like this: How make it ideologically correct?
I want some static pages of application to be rendered from XML i.e. i
I have some long-running operations that number in the hundreds. At the moment they
We have a data model that has some requirements. I would like to find
I have a long operation running in the background, like uploading stuff, converting images,
I'm working with some code that does a bunch of asynchronous operating with various
I need some operations that are to be performed on large long[]s to be

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.