Help with basic Threading / Message handling App

by EightBitSpade » Sat, 02 Oct 2010 02:14:41 GMT

Sponsored Links
 ello everyone! I recently started learning to make android apps, and
I decided that I needed to learn how to go about working with multiple
threads (I eventually want to make fairly simple, 2D games). So I
figured I would design a super-simple app that would use two threads
and send messages from one thread to the other.

Here's the app I came up with: It should simply display one text view
on the screen. A handler should be set up in the main thread that will
respond to messages - if the message sent contains the value 1, then
it should set the text of this text view to "Start." If it receives a
2, then it should change that text to "It worked!"

That message should come from the other, secondary thread, which
should perform like this: It contains one variable, an int named
counter that starts off at 0. Then, if the counter is 0, the counter
is set to 1, and the value of counter is sent to the main thread
handler in the what field of a message called m (where the main thread
handler should receive it and change the text view text to "Start.").
Then, the thread should sleep for 5 seconds. When it resumes and runs
a second time, it should change the value of counter to 2, and send
that to the main thread handler, which would then change the text view
text to "It worked!"

Here is my code:

---------- ----------------------------------
package com.thread.test;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class threadTest extends Activity {

TextView myTextView = null;

Thread refreshThread = null;

Handler updateHandler = new Handler()

//Handle message routine
//This gets called on every message that is recieved

public void handleMessage(Message msg)
if (msg.what == 1)
if (msg.what == 2)
myTextView.setText("It Worked!");

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
//View referencing
this.myTextView = (TextView)findViewById(;


//Thread creation
this.refreshThread = new Thread(new updateThread());

class updateThread implements Runnable{
private int counter = 0;
public void run()
Message m = new Message();
if (this.counter == 1)
this.counter = 2;
m.what = this.counter;
if (this.counter == 0)
this.counter = 1;
m.what = this.counter;

Help with basic Threading / Message handling App

by TreKing » Sat, 02 Oct 2010 02:54:59 GMT



If you look at the message, it also tells you exactly what line it's
crashing on. So it's only a matter of using your debugger and stopping on
the line that's it's throwing the exception on and seeing what's null at
that point. From there, "walk back" to see what calls what along the path to
that point to see where the variable that is null is being sent from and why
it's not initialized.

TreKing <> ; - Chicago
transit tracking app for Android-powered devices


Sponsored Links

Help with basic Threading / Message handling App

by Prakash Iyer » Sat, 02 Oct 2010 04:34:10 GMT

 Can you call the findViewById before the setContentView? If not the text
view member is null and that's your problem...


Help with basic Threading / Message handling App

by Miguel Morales » Sat, 02 Oct 2010 05:25:25 GMT


The correct order for your onCreate() method is like:

this.myTextView = (TextView)findViewById(;

I *believe* that your myTextView ends up being null.  Your
handler/thread code looks ok.


Other Threads

1. Making SmartCard feature available in Google Nexus S

Hi all,

I undestand that smartcard feature is not available for developers.

I readed in this website that is possible to available it:

The clues that I get to make it available is:
"Download the source (actually from CyanogenMod 7 to have the full
build environment for the new Nexus S), make the appropriate changes
to the code, recompile everything and put it back into the phone and
it works Nexus S supports card emulation and SWP!"

Also I need to root it... I know..

The question is: are there anybody who could provide more info about

Thanks in advantage


2. Remote views set Background Drawable

I have a remote view (I.e. I am using a widget), and I want to change
the background programaticaly. Basically the background is s shape
with rounded corners and I simply want to change the colour. I can
change the colour using the code below
views.setInt(, "setBackgroundColor",;

but really I want to set the background to a drawable I create at
runtime (rather than from a resource file. There doesn't seem to be a
method available to do this, any ideas?

Widget layout shown below for reference, the bit I want to change the
background of is stats_w_name.

  android:orientation="vertical" android:layout_height="72dp"
    <TextView android:layout_width="fill_parent"
android:gravity="center" android:background="@drawable/box1"
android:id="@+id/stats_w_name" android:text="Error"
    <TextView android:layout_width="fill_parent" android:id="@+id/
stats_w_value" android:layout_height="34dp" android:gravity="center"
android:text="1234567.00" android:textSize="28sp"
    <TextView android:layout_width="fill_parent"
android:text="1/1/2000" android:gravity="center" android:id="@+id/
stats_w_time" android:layout_height="20dp" android:textSize="10sp"



3. Many apps disappeared in Android Market this morning

4. ADT export wizard with Proguard obfuscation enabled does not work

5. aidl linking issue, in INetworkQueryService.aidl: couldn't find import for class

6. Looking for tutor for Android Dev, will pay - Web Conferencing, phone, or F2F...

7. Android can't prompt busybox httpd authentication for login with webwiew