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:

---------- threadTest.java ----------------------------------
package com.thread.test;

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

public class threadTest extends Activity {

//Fields
TextView myTextView = null;

Thread refreshThread = null;

//Handler
Handler updateHandler = new Handler()
{

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

//@Override
public void handleMessage(Message msg)
{
if (msg.what == 1)
{
myTextView.setText("Start");
}
if (msg.what == 2)
{
myTextView.setText("It Worked!");
}
super.handleMessage(msg);
}
};

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

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

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

class updateThread implements Runnable{
private int counter = 0;
//@Override
public void run()
{
while(!Thread.currentThread().isInterrupted())
{
Message m = new Message();
if (this.counter == 1)
{
this.counter = 2;
m.what = this.counter;
threadTest.this.updateHandler.sendMessage(m);
}
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


 



Yes.



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 < http://sites.google.com/site/rezmobileapps/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


 Correct,

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

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.myTextView = (TextView)findViewById(R.id.myTextView);

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






>>



Other Threads

1. Editing the incoming call dialog

Can anyone offer some advice on how I can add/replace text on the
dialog displayed when an incoming call is ringing? I guess I need a
way to get a handle on the view.

Thanks
Rich

--~--~---------~--~----~------------~-------~--~----~

2. ActivityNotFoundException..

Hi,

I want to launch a new Activity from my current Activity.. so i do:

<activity android:name=".Device" android:label="@string/device_name">
   <intent-filter>
        <action android:name="android.intent.action.EDIT" />
   </intent-filter>
</activity>

and in my code ..

Intent intent = new Intent(Intent.ACTION_EDIT);
intent.putExtra(Device.DEVICE, getItem(position).toString());
((Activity) getContext()).startActivityForResult(intent,
Device.FROM_LIST);

And i receive and ActivityNotFoundException :(

I try it to create an Intent with the Device.class like that :
Intent intent = new Intent(getContext(), Device.class);

And it's working... why? :(

--~--~---------~--~----~------------~-------~--~----~

3. AIDL in same process

4. initializing chosen item for ListView.CHOICE_MODE_SINGLE

5. How to do a ScrollView in a LinearLayout correctly

6. Very Newbie Question

7. Bluetooth PAN tethering