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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:31:38+00:00 2026-06-07T09:31:38+00:00

I am trying to make a remote control for a Google TV. I want

  • 0

I am trying to make a remote control for a Google TV.
I want to change the text I have in a layout (TextView statusText) with connected when the device has successfully connected. But I get an exception when I try to do this:
“07-07 22:42:20.870: E/AndroidRuntime(5750):android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
“

Appreciate any help/pointers

Here is my MainActivity.java and main.xml:

MainActivity.java:

package uk.co.mypack.gtvremote;

//imports removed for paste

public class MainActivity extends Activity implements ClientListener{

    private AnymoteSender anymoteSender;
    private TextView statusText;
    protected AnymoteClientService mAnymoteClientService;
    private static String statusPrefix = "Status: ";
    private Context mContext;
    private ProgressBar progressBar;

    private Handler handler;
    private TouchHandler touchPadHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        progressBar = (ProgressBar) findViewById(R.id.a_progressbar);
        progressBar.setVisibility(View.VISIBLE);

        mContext = this;



        ImageButton upArrowButton = (ImageButton) findViewById(R.id.upArrow);
        ImageButton leftArrowButton = (ImageButton) findViewById(R.id.leftArrow);
        ImageButton centreButton = (ImageButton) findViewById(R.id.centreButton);
        ImageButton rightArrowButton = (ImageButton) findViewById(R.id.rightArrow);
        ImageButton downArrowButton = (ImageButton) findViewById(R.id.downArrow);

        upArrowButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyEvent(KeyEvent.KEYCODE_DPAD_UP);
            }
        });

        leftArrowButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyEvent(KeyEvent.KEYCODE_DPAD_LEFT);
            }
        });

        centreButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyEvent(KeyEvent.KEYCODE_DPAD_CENTER);
            }
        });

        rightArrowButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyEvent(KeyEvent.KEYCODE_DPAD_RIGHT);
            }
        });

        downArrowButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN);
            }
        });


        handler = new Handler();

        // Bind to the AnymoteClientService
        Intent intent = new Intent(mContext, AnymoteClientService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        statusText = (TextView) findViewById(R.id.statusText);


    }

    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {
        /*
         * ServiceConnection listener methods.
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            mAnymoteClientService = ((AnymoteClientService.AnymoteClientServiceBinder) service)
                    .getService();
            mAnymoteClientService.attachClientListener(MainActivity.this);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mAnymoteClientService.detachClientListener(MainActivity.this);
            mAnymoteClientService = null;   
        }

    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onConnected(AnymoteSender anymoteSender) {
        if (anymoteSender != null) {
            // Send events to Google TV using anymoteSender.
            // save handle to the anymoteSender instance.
            this.anymoteSender = anymoteSender;

            //THIS IS WHERE I AM TRYING TO SET THE TEXTVIEW

            TextView localStatusText = (TextView) findViewById(R.id.statusText);
            localStatusText.setText(statusPrefix + "Connected to GoogleTV");

            //ABOVE IS WHERE I AM TRYING TO SET THE TEXTVIEW

            // Attach touch handler to the touchpad view
            touchPadHandler = new TouchHandler(
                    findViewById(R.id.touchPad), Mode.POINTER_MULTITOUCH, anymoteSender);

        } else {
            statusText.setText(statusPrefix + "Connection attempt failed, cant find send handler");
            //attempt to connect again?
            //attemptToConnect();
        }

        // Hide the progressBar once connection to Google TV is established.
        handler.post(new Runnable() {
            public void run() {
                progressBar.setVisibility(View.INVISIBLE);
            }
        });
    }

    @Override
    public void onDisconnected() {
        // show message to tell the user about disconnection.
        statusText.setText(statusPrefix + "Disconnected");
        // Try to connect again if needed. This may be need to be done via button
        attemptToConnect();
        this.anymoteSender = null;

    }

    @Override
    public void onConnectionError() {
        // show message to tell the user about disconnection.
        statusText.setText(statusPrefix + "Connection error encountered");
        // Try to connect again if needed.
        attemptToConnect();
        this.anymoteSender = null;

    }

    @Override
    protected void onDestroy() {
        if (mAnymoteClientService != null) {
            mAnymoteClientService.detachClientListener(this);
        }
        unbindService(mConnection);
        super.onDestroy();
    }

    public void attemptToConnect()
    {
        //stub to invoke connection attempt
    }

    private void sendKeyEvent(final int keyEvent) {
        // create new Thread to avoid network operations on UI Thread
        if (anymoteSender == null) {
            Toast.makeText(MainActivity.this, "Waiting for connection",
                    Toast.LENGTH_LONG).show();
            return;
        }
        anymoteSender.sendKeyPress(keyEvent);
    }
}

Main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/control_message"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="@dimen/padding_medium"
        android:text="@string/control_msg"
        android:textSize="90dp"
        tools:context=".MainActivity" />

    <LinearLayout
        android:id="@+id/middlePanel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/statusText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Status: Disconnected - startup"
            android:textSize="20dp" />

        <ImageView
            android:id="@+id/touchPad"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/greysquare" 
            />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical" >

            <ImageButton
                android:id="@+id/upArrow"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:background="@drawable/blackuparrow" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/leftArrow"
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:background="@drawable/blackleftarrow" />

                <ImageButton
                    android:id="@+id/centreButton"
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:background="@drawable/emptycircle"
                    android:paddingBottom="10dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="10dp" />

                <ImageButton
                    android:id="@+id/rightArrow"
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:background="@drawable/blackrightarrow" />
            </LinearLayout>

            <ImageButton
                android:id="@+id/downArrow"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:background="@drawable/blackdownarrow" />
        </LinearLayout>
    </LinearLayout>
<ProgressBar
        android:id="@+id/a_progressbar"
        style="@android:style/Widget.ProgressBar.Large"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</LinearLayout>
  • 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-07T09:31:39+00:00Added an answer on June 7, 2026 at 9:31 am

    The onConnected() callback is not called on the Main UI thread, but on a separate thread that is used by the Service. So it is not able to access the TextView created in Main UI thread. What you should do is create a Handler in the main UI thread and then use that handler to post a runnable that makes changes to the TextView. You can read more about Handlers on the Android developer site.

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

Sidebar

Related Questions

The goal: Remote control of an Android device. I'm trying to make an alternate
I have a Rails app and trying to make a remote form, so far
I'm trying to make an AjaxControlToolkit Accordion control change the header style on mouseover
I'm trying make an entity with doctrine that has three associations with other entities
Trying to make a simple number clicker control for BlackBerry 6/7, like this: At
I'm trying to make Spring automatically register my remote destinations by reading its annotations.
I'm trying to make a class to handle invoking methods on a remote server
I am trying to make a many to one relationship and want to be
I'm trying to make a local repo act as a remote with the name
I am trying to make a remote procedure call of GWT RPC service. I

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.