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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:18:11+00:00 2026-06-17T16:18:11+00:00

i put a couple of onclick methods inside of a run() thread method in

  • 0

i put a couple of onclick methods inside of a run() thread method in a sockets program for android.

they don’t seem to work. is possible to do put these onclick listeners inside of the run() method or it it now possible. not sure why the buttons are not generating a response when clicking on them. the message is not getting sent

here is the two onclick listeners that i am using. one is sendA and the other is sendB and they send the text messages testA and testB

public class ClientThread implements Runnable {

    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
            Log.d("ClientActivity", "C: Connecting...");
            Socket socket = new Socket(serverAddr, 8080);
            connected = true;
            while (connected) {
                try {
                    Log.d("ClientActivity", "C: Sending command.");
                    final PrintWriter out = new PrintWriter(
                        new BufferedWriter(new OutputStreamWriter(
                            socket.getOutputStream())), true);
                    sendA.setOnClickListener(new View.OnClickListener() {

                        // ananamous inner class override for on click
                        public void onClick(View v) {
                            out.println("testA");
                        }
                    });
                    sendB.setOnClickListener(new View.OnClickListener() {

                        // ananamous inner class override for on click
                        public void onClick(View v) {
                            out.println("testB");
                        }
                    });
                    // where you issue the commands
                    // out.println("testX");
                    Log.d("ClientActivity", "C: Sent.");
                } catch (Exception e) {
                    Log.e("ClientActivity", "S: Error", e);
                }
            }
            socket.close();
            Log.d("ClientActivity", "C: Closed.");
        } catch (Exception e) {
            Log.e("ClientActivity", "C: Error", e);
            connected = false;
        }
    }
}
  • 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-17T16:18:13+00:00Added an answer on June 17, 2026 at 4:18 pm

    I make a simple demo and it works;

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HelpActivity" >
    
    <Button
        android:id="@+id/a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A" />
    <Button
        android:id="@+id/b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/a"
        android:text="B" />
    
    </RelativeLayout>
    

    public class HelpActivity extends Activity {
    
    private Button aBtn;
    private Button bBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        aBtn = (Button) findViewById(R.id.a);
        bBtn = (Button) findViewById(R.id.b);
        new Thread(new Runnable() {
    
            @Override
            public void run() {
                // TODO Auto-generated method stub
                aBtn.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        Log.v("a", "a");
                    }
                });
                bBtn.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {   
                        Log.v("b", "b");
                    }
                });
    
            }
        }).start();
    }
    
    }
    

    when I click the button ,there are logs in LogCat!
    LogCat View

    So,maybe your situation is caused by sequence.I mean when you click the button the listener hasn’t been attached to the button.

    Well,if you insist set listener on main thread ,you can use handler
    This Handler class should be static or leaks might occur: IncomingHandler

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

Sidebar

Related Questions

I created a couple of extended methods in a VB.NET module. I put them
I am looking into a basic DOM layout that lets me put a couple
Put simply: it causes a massive memory leak within an hour if I work
I have a couple of sites that are currently under development and I've put
How do you take a couple of data tables and put them in a
We have a couple of DLLs that need to be put in the GAC
Hi I am working with android and I'm trying to put an if statement
I have an HttpModule that I have put together from cobbling a couple of
I have couple of bat files which run further files to build the project.
I have a couple of methods being fired by each request by before_filter. I've

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.