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. 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:
http://www.nearfieldcommunicationsworld.com/2011/02/13/35913/uncovered-the-hidden-nfc-potential-of-the-google-nexus-s-and-the-nokia-c7/

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
this?

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(R.id.stats_w_name, "setBackgroundColor",
android.graphics.Color.GREEN);

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.

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_height="72dp"
android:layout_width="146dp">
    <TextView android:layout_width="fill_parent"
android:gravity="center" android:background="@drawable/box1"
android:id="@+id/stats_w_name" android:text="Error"
android:layout_height="23dp"></TextView>
    <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"
android:background="@drawable/widget_figure_background"
android:textColor="#000000"></TextView>
    <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"
android:background="@drawable/bottom_box"></TextView>

</LinearLayout>

-- 

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 com.android.phone.INetworkQueryServiceCallback

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