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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:00:43+00:00 2026-05-20T08:00:43+00:00

I am using a thread and a handler in android. The app works fine

  • 0

I am using a thread and a handler in android. The app works fine as long as i dont any function from outside the activity. But if i call some funcyion from outside the activity from inside a thread, it gives NullPointerException.

package com.prog;

import com.API.TextBoxCheck;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ProgressBarExample extends Activity {
    private Handler handler = new Handler();
    int i;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Button btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener(){
            TextView tv=(TextView)findViewById(R.id.textView1);

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Thread thread = new Thread(null, doBackgroundThreadProcessing,
                "Background");
                thread.start();
            }});
        Button stopBtn=(Button)findViewById(R.id.button2);
        stopBtn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            finish();
            }});
    }

    private Runnable doBackgroundThreadProcessing = new Runnable() {
    public void run() {
    try {
        backgroundThreadProcessing();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
    };

    private void backgroundThreadProcessing() throws InterruptedException {

        TextView tv=(TextView)findViewById(R.id.textView1);

         i=0;
        while(i<100)
        {
            handler.post(doUpdateGUI);
        Thread.sleep(50);

        i++;    
        }
        EditText et=(EditText)findViewById(R.id.editText1);

        TextBoxCheck tbc = new com.API.TextBoxCheck();
        String reply=tbc.TextBoxChecker(et.getText().toString(),10);
        Log.d("thread", reply);
    }


    private Runnable doUpdateGUI = new Runnable() {
    public void run() {
    updateGUI();
    }

    private void updateGUI() {
        // TODO Auto-generated method stub
        TextView tv=(TextView)findViewById(R.id.textView1);
        tv.setText(i+"%");

    }
    };
}

I have left out the code of textBoxCheck becoz i think it may be unnecesarry here.
Please help me on this.

PS. : I also tried using AsyncTask but the same problem occurs.

  • 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-20T08:00:44+00:00Added an answer on May 20, 2026 at 8:00 am

    You are not on UI thread. You must be on UI thread to operate on any UI items. Create a handler on the UI thread and call your backgroundThreadProcessing(); from the handler and not from a non-UI thread.

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

Sidebar

Related Questions

What is the difference between using a new thread and using a thread from
Sample code that shows how to create threads using MFC declares the thread function
When using a thread pool and its queuecallbackitem, can I not pass in a
Is writing to stdout using printf thread-safe on Linux? What about using the lower-level
On another thread I saw a answer to a sql problem using Oracle's analytics.
I am using an instance of ManualResetEvent to control thread access to a resource
Using TeamCity, I'm trying to get a (TestAutomationFX) test that requires an STA thread
If I'm using ConcurrentHashMap (where the put is thread safe) , and I supply
I am experiencing a crash while using the Boost.Spirit and Boost.Thread libraries in my
I trying to put together an Android app that will take a picture and

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.