Android communication from child thread to main thread
<p>I have an android application with a main thread and a child thread. I need the child thread to send messages periodically to the main thread. The main thread in turn updates the UI. I have tried to do this process using a handler, obtainMessage() and sendMessage() etc but my application fails when the main thread updates the textView even though I print the message received to the log and it's perfectly fine.</p>

<p>So I tried another way of posting a runnable in the child thread to update the UI</p>

<p><strong>In the main thread:</strong></p>

<pre><code>public class Example extends Activity {
    public TextView mMatchesText;
    public Handler mHandler;  
    private ServerConnection conn;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mMatchesText = (TextView) Example.this.findViewById(R.id.matches);
        mMatchesText.setText("Matches:\n");     /*this text appears when I run the app*/
        mHandler = new Handler();
        SessionEvents.addAuthListener(new SampleAuthListener());

 public class SampleAuthListener implements AuthListener {

        public void onAuthSucceed() {
            Example.this.runOnUiThread(new Runnable() {
                    public void run() {
                        conn = new ServerConnection(mProfile.toString());
                    }
                });
        }
   }
</code></pre>

<p><strong>In the child thread (ServerConnection.java):</strong></p>

<pre><code>/* constructor */
public ServerConnection()
{
    runner = new Thread(this);
    runner.start();
}


public void run()
{
    String fromServer;

    /** Establish connection to the server */

    /** Wait for messages from the server */
    while((fromServer = inFromServer.readLine()) != null)
    {
        mHandler.post(new Runnable(){
            @Override
            public void run(){
                Log.e("MY APP", fromServer);
                mMatchesText.setText(fromServer); 
            }
        });
    }
}
</code></pre>

<p>The application again fails at line 56 which is <strong>mMatchesText.setText(fromServer);</strong> 
formServer is not null because I print it to the LogCat and it indeed contains the data sent by the server. </p>

<pre><code>    11-24 08:02:49.622: ERROR/AndroidRuntime(14571): FATAL EXCEPTION: main
    11-24 08:02:49.622: ERROR/AndroidRuntime(14571): java.lang.NullPointerException

    11-24 08:02:49.622: ERROR/AndroidRuntime(14571): at com.facebook.android.ServerConnection$1.run(ServerConnection.java:56) &lt;--this is mMatches..

    11-24 08:02:49.622: ERROR/AndroidRuntime(14571): at android.os.Handler.handleCallback(Handler.java:587)

    11-24 08:02:49.622: ERROR/AndroidRuntime(14571): at android.os.Handler.dispatchMessage(Handler.java:92)

    11-24 08:02:49.622: ERROR/AndroidRuntime(14571): at android.os.Looper.loop(Looper.java:130)

    11-24 08:02:49.622: ERROR/AndroidRuntime(14571): at 
</code></pre>

<p>android.app.ActivityThread.main(ActivityThread.java:3683)</p>

<p>Just in case it helps, the mMatchesText is declared in main.xml as follows:</p>

<pre><code>&lt;TextView android:id="@+id/matches"
android:textColor="@drawable/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Login"
android:text=""
/&gt;
</code></pre>

<p>Can someone help?</p>
