[android-developers] Help with basic Threading / Message handling App
Hello 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; threadTest.this.updateHandler.sendMessage(m); } try{ Thread.sleep(5000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } }//End class updateThread } --- Unfortunately, it does not work. As soon as I try to run it, I get a force close error. i checked my logcat out, and this is what I found: --- My logcat 09-30 00:24:47.883: INFO/ActivityManager(71): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x1020 cmp=com.thread.test/.threadTest } 09-30 00:24:48.543: WARN/ActivityManager(71): Activity pause timeout for HistoryRecord{43f9cf00 com.android.launcher/ com.android.launcher2.Launcher} 09-30 00:24:48.783: INFO/ActivityManager(71): Start proc com.thread.test for activity com.thread.test/.threadTest: pid=250 uid=10039 gids={1015} 09-30 00:24:50.453: DEBUG/dalvikvm(71): GREF has increased to 301 09-30 00:24:50.483: DEBUG/AndroidRuntime(250): Shutting down VM 09-30 00:24:50.483: WARN/dalvikvm(250): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 09-30 00:24:50.552: ERROR/AndroidRuntime(250): FATAL EXCEPTION: main 09-30 00:24:50.552: ERROR/AndroidRuntime(250): java.lang.NullPointerException 09-30 00:24:50.552: ERROR/AndroidRuntime(250): at com.thread.test.threadTest$1.handleMessage(threadTest.java:33) 09-30 00:24:50.552: ERROR/AndroidRuntime(250): at
Re: [android-developers] Help with basic Threading / Message handling App
On Wed, Sep 29, 2010 at 9:37 PM, EightBitSpade spectegu...@yahoo.comwrote: Unfortunately, I don't exactly what this means, but I gather that the problem comes from an uncaught exception occuring in threadid=1, that it was a java.lang.NullPointerException, and that it had something to do with handleMessage. Am i correct in this interpretation? Yes. And can anyone give me pointers on what I'm doing wrong here? 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 -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Help with basic Threading / Message handling App
Can you call the findViewById before the setContentView? If not the text view member is null and that's your problem... On Fri, Oct 1, 2010 at 2:54 PM, TreKing treking...@gmail.com wrote: On Wed, Sep 29, 2010 at 9:37 PM, EightBitSpade spectegu...@yahoo.comwrote: Unfortunately, I don't exactly what this means, but I gather that the problem comes from an uncaught exception occuring in threadid=1, that it was a java.lang.NullPointerException, and that it had something to do with handleMessage. Am i correct in this interpretation? Yes. And can anyone give me pointers on what I'm doing wrong here? 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 -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Help with basic Threading / Message handling App
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. On Fri, Oct 1, 2010 at 1:33 PM, Prakash Iyer thei...@gmail.com wrote: Can you call the findViewById before the setContentView? If not the text view member is null and that's your problem... On Fri, Oct 1, 2010 at 2:54 PM, TreKing treking...@gmail.com wrote: On Wed, Sep 29, 2010 at 9:37 PM, EightBitSpade spectegu...@yahoo.com wrote: Unfortunately, I don't exactly what this means, but I gather that the problem comes from an uncaught exception occuring in threadid=1, that it was a java.lang.NullPointerException, and that it had something to do with handleMessage. Am i correct in this interpretation? Yes. And can anyone give me pointers on what I'm doing wrong here? 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 -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- ~ Jeremiah:9:23-24 Android 2D MMORPG: http://developingthedream.blogspot.com/, http://diastrofunk.com, http://www.youtube.com/user/revoltingx -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en