[android-developers] Re: Help...Exception in the Background Thread :Can't create handler inside thread that has not called Looper.prepare()
Hi all, Thanks 4 ur reply. Actually I want to call some methods of one Activity (test.check.Test1.java) from another activity (my current activity) at runtime in the separate Thread. In the main GUI Thread this works fine. This Test1.java activity resides in the test.check.apk file which is stored in the /sdcard. This is simply activity class with only one function xyz() (which I want to call) and returns a String Hellooo Man as follows package test.check; import android.app.Activity; import android.os.Bundle; public class Test1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public String xyz(){ return Hellooo Man; } }. But during the execution of this function from another activity (another .apk) at runtime in the separate Thread causes exception at this line Object o = c.newInstance(); which I came to know during debugging , otherwise class is Loaded nicely and also loaded method xyz() without any problem. But to execute that method I need to create Obect instance and then Method.invoke(o); I hope u got it now. Dianne:: Exception race is as follows, 04-08 11:57:08.802: ERROR/AndroidRuntime(240): Uncaught handler: thread Thread-8 exiting due to uncaught exception 04-08 11:57:08.812: ERROR/AndroidRuntime(240): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.os.Handler.init(Handler.java:111) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.app.Activity.init(Activity.java:660) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at test.check.Test1.init(Test1.java:6) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at java.lang.Class.newInstance(Native Method) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.giude.SocketFTP.SocketFTP.readTheFile(SocketFTP.java:128) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.giude.SocketFTP.SocketFTP$1.run(SocketFTP.java:50) Mariano : I have not used Looper.prepare(), Is it required?? If yes then where and how to define handler for that? On Apr 7, 10:20 pm, Dianne Hackborn hack...@android.com wrote: Also you need to look at the exception stack trace. It will show you who is trying to create a Handler. On Tue, Apr 7, 2009 at 9:44 AM, Streets Of Boston flyingdutc...@gmail.comwrote: But do you want a Looper and Handler in your thread 't'? Why do you load it in another thread? Are you planning in dispatching and handling messages in this thread 't'? If not, don't start adding all this stuff just to make the run-time happy. Put a breakpoint in your default constructor of your test.check.Test1 class and see which line of code needs a Handler or Looper. What are you trying to accomplish? It looks like you're trying to load and construct a class from an APK (check.apk). Are you initializing an Activity from this APK called 'Test1'? If so, I understand why it may need a looper/handler. But if you're loading an Activity (Test1) within another Activity (the one trying to load check.apk), you may get into a heap of other trouble. But i'm not an expert on this at all. Google engineers will know much more about this. On Apr 7, 10:55 am, Ask asifk1...@gmail.com wrote: Hi Streets, I dont think this is the case. Same code executes nicely If I call that method in the onCreate() method (i.e. main thread itself) problem occurs in the Object creation if I will call this method readTheFile() in the Thread. Can you give any info. regarding how I can define Looper and Handler in my code to execute it without any error.??? On Apr 7, 7:00 pm, Streets Of Boston flyingdutc...@gmail.com wrote: It could be that during the construction of class test.check.Test1, a Handler or something similar (i.e. something needing the main-thread's message-queue) is constructed. Look at your test.check.Test1 implementation and see if this is the case. If this is the case, you will get an exception if the constructor of test.check.Test1 is executed on any other thread than the main-thread with the message queue. You would have gotten the same exception if you would have called 'new test.check.Test1()'. On Apr 7, 9:17 am, Ask asifk1...@gmail.com wrote: Is there any handler needed for that?? Any Idea??? On Apr 7, 3:58 pm, Ask asifk1...@gmail.com wrote: Hi, I am using Reflection APIs in the background thread to call a method dynamically from another application but I am getting the Exception: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at a line : Object o = c.newInstance(); my code in the Thread is as
[android-developers] Re: Help...Exception in the Background Thread :Can't create handler inside thread that has not called Looper.prepare()
Mariano : I have not used Looper.prepare(), Is it required?? If yes then where and how to define handler for that? I honestly don't understand what you're trying to achieve, so it's hard to answer that question. I once tried to inflate GUIs in a separate thread for performance reasons. I didn't touch any Window at that point, but when inflating I got the same error message and I just ran Looper.prepare() in my Thread and all was well. On Wed, Apr 8, 2009 at 8:44 AM, Ask asifk1...@gmail.com wrote: Hi all, Thanks 4 ur reply. Actually I want to call some methods of one Activity (test.check.Test1.java) from another activity (my current activity) at runtime in the separate Thread. In the main GUI Thread this works fine. This Test1.java activity resides in the test.check.apk file which is stored in the /sdcard. This is simply activity class with only one function xyz() (which I want to call) and returns a String Hellooo Man as follows package test.check; import android.app.Activity; import android.os.Bundle; public class Test1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public String xyz(){ return Hellooo Man; } }. But during the execution of this function from another activity (another .apk) at runtime in the separate Thread causes exception at this line Object o = c.newInstance(); which I came to know during debugging , otherwise class is Loaded nicely and also loaded method xyz() without any problem. But to execute that method I need to create Obect instance and then Method.invoke(o); I hope u got it now. Dianne:: Exception race is as follows, 04-08 11:57:08.802: ERROR/AndroidRuntime(240): Uncaught handler: thread Thread-8 exiting due to uncaught exception 04-08 11:57:08.812: ERROR/AndroidRuntime(240): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.os.Handler.init(Handler.java:111) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.app.Activity.init(Activity.java:660) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at test.check.Test1.init(Test1.java:6) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at java.lang.Class.newInstance(Native Method) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.giude.SocketFTP.SocketFTP.readTheFile(SocketFTP.java:128) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.giude.SocketFTP.SocketFTP$1.run(SocketFTP.java:50) Mariano : I have not used Looper.prepare(), Is it required?? If yes then where and how to define handler for that? On Apr 7, 10:20 pm, Dianne Hackborn hack...@android.com wrote: Also you need to look at the exception stack trace. It will show you who is trying to create a Handler. On Tue, Apr 7, 2009 at 9:44 AM, Streets Of Boston flyingdutc...@gmail.comwrote: But do you want a Looper and Handler in your thread 't'? Why do you load it in another thread? Are you planning in dispatching and handling messages in this thread 't'? If not, don't start adding all this stuff just to make the run-time happy. Put a breakpoint in your default constructor of your test.check.Test1 class and see which line of code needs a Handler or Looper. What are you trying to accomplish? It looks like you're trying to load and construct a class from an APK (check.apk). Are you initializing an Activity from this APK called 'Test1'? If so, I understand why it may need a looper/handler. But if you're loading an Activity (Test1) within another Activity (the one trying to load check.apk), you may get into a heap of other trouble. But i'm not an expert on this at all. Google engineers will know much more about this. On Apr 7, 10:55 am, Ask asifk1...@gmail.com wrote: Hi Streets, I dont think this is the case. Same code executes nicely If I call that method in the onCreate() method (i.e. main thread itself) problem occurs in the Object creation if I will call this method readTheFile() in the Thread. Can you give any info. regarding how I can define Looper and Handler in my code to execute it without any error.??? On Apr 7, 7:00 pm, Streets Of Boston flyingdutc...@gmail.com wrote: It could be that during the construction of class test.check.Test1, a Handler or something similar (i.e. something needing the main-thread's message-queue) is constructed. Look at your test.check.Test1 implementation and see if this is the case. If this is the case, you will get an exception if the constructor of test.check.Test1 is executed on any other thread than the main-thread with the message queue.
[android-developers] Re: Help...Exception in the Background Thread :Can't create handler inside thread that has not called Looper.prepare()
yess right, in short i was trying to instantiate a view comp. Still not clear logic for using Looper. might be Google engineers can answer this. On Apr 8, 6:14 pm, Mariano Kamp mariano.k...@gmail.com wrote: Glad it works. Well, what you were trying to instantiate was a View Component, weren't you? I don't think it matters that you were using reflection for it.Why a view component needs the looper before it is attached to an active Window, I don't know. On Wed, Apr 8, 2009 at 2:31 PM, Ask asifk1...@gmail.com wrote: It works nice after I had added only one line : Looper.prepare(); in the begining of the public void run(); method. But still I could not understand, How Looper.prepare(); provides a handler to Object o = c.newInstance (); Line??? In short, got the solution but wants some clearification. Regards, Asif On Apr 8, 11:44 am, Ask asifk1...@gmail.com wrote: Hi all, Thanks 4 ur reply. Actually I want to call some methods of one Activity (test.check.Test1.java) from another activity (my current activity) at runtime in the separate Thread. In the main GUI Thread this works fine. This Test1.java activity resides in the test.check.apk file which is stored in the /sdcard. This is simply activity class with only one function xyz() (which I want to call) and returns a String Hellooo Man as follows package test.check; import android.app.Activity; import android.os.Bundle; public class Test1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public String xyz(){ return Hellooo Man; } }. But during the execution of this function from another activity (another .apk) at runtime in the separate Thread causes exception at this line Object o = c.newInstance(); which I came to know during debugging , otherwise class is Loaded nicely and also loaded method xyz() without any problem. But to execute that method I need to create Obect instance and then Method.invoke(o); I hope u got it now. Dianne:: Exception race is as follows, 04-08 11:57:08.802: ERROR/AndroidRuntime(240): Uncaught handler: thread Thread-8 exiting due to uncaught exception 04-08 11:57:08.812: ERROR/AndroidRuntime(240): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.os.Handler.init(Handler.java:111) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.app.Activity.init(Activity.java:660) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at test.check.Test1.init(Test1.java:6) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at java.lang.Class.newInstance(Native Method) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.giude.SocketFTP.SocketFTP.readTheFile(SocketFTP.java:128) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.giude.SocketFTP.SocketFTP$1.run(SocketFTP.java:50) Mariano : I have not used Looper.prepare(), Is it required?? If yes then where and how to define handler for that? On Apr 7, 10:20 pm, Dianne Hackborn hack...@android.com wrote: Also you need to look at the exception stack trace. It will show you who is trying to create a Handler. On Tue, Apr 7, 2009 at 9:44 AM, Streets Of Boston flyingdutc...@gmail.comwrote: But do you want a Looper and Handler in your thread 't'? Why do you load it in another thread? Are you planning in dispatching and handling messages in this thread 't'? If not, don't start adding all this stuff just to make the run-time happy. Put a breakpoint in your default constructor of your test.check.Test1 class and see which line of code needs a Handler or Looper. What are you trying to accomplish? It looks like you're trying to load and construct a class from an APK (check.apk). Are you initializing an Activity from this APK called 'Test1'? If so, I understand why it may need a looper/handler. But if you're loading an Activity (Test1) within another Activity (the one trying to load check.apk), you may get into a heap of other trouble. But i'm not an expert on this at all. Google engineers will know much more about this. On Apr 7, 10:55 am, Ask asifk1...@gmail.com wrote: Hi Streets, I dont think this is the case. Same code executes nicely If I call that method in the onCreate() method (i.e. main thread itself) problem occurs in the Object creation if I will call this method readTheFile() in the Thread. Can you give any info. regarding how I can define Looper and Handler in my code to execute it
[android-developers] Re: Help...Exception in the Background Thread :Can't create handler inside thread that has not called Looper.prepare()
On Wed, Apr 8, 2009 at 6:51 AM, Ask asifk1...@gmail.com wrote: yess right, in short i was trying to instantiate a view comp. Still not clear logic for using Looper. might be Google engineers can answer this. Do it on the main thread. If you want to actually run UI on another thread, read up on Looper and Handler, do all the stuff such as calling Looper.prepare() in your thread, and then redesign your thread logic to be messages based on not do work more than a few seconds and all of the other constraints on a thread that is running UI. -- Dianne Hackborn Android framework engineer hack...@android.com Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-developers] Re: Help...Exception in the Background Thread :Can't create handler inside thread that has not called Looper.prepare()
Maybe your example is simplified, but if this method you want to call (xyz()) is really this simple (i.e. it does not really need a whole activity and all its functionality (Looper and such)), refactor your code and put this 'xyz()' in a class that is not an activity and refer to this class by both activities. public class Test1 extends Activity { private CommonClass mCommon = new CommonClass(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public String xyz(){ return mCommon.xyz(); } }. public class CommonClass { public String xyz(){ return Hellooo Man; } } And from the other activity: ... ... c = loader.loadClass(test.check.CommonClass); Log.i(See, Test Class Found ); Method method = c.getMethod(xyz, null); Object o = c.newInstance(); // This line throws String s = (String) method.invoke(o); ... ... On Apr 8, 2:44 am, Ask asifk1...@gmail.com wrote: Hi all, Thanks 4 ur reply. Actually I want to call some methods of one Activity (test.check.Test1.java) from another activity (my current activity) at runtime in the separate Thread. In the main GUI Thread this works fine. This Test1.java activity resides in the test.check.apk file which is stored in the /sdcard. This is simply activity class with only one function xyz() (which I want to call) and returns a String Hellooo Man as follows package test.check; import android.app.Activity; import android.os.Bundle; public class Test1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public String xyz(){ return Hellooo Man; } }. But during the execution of this function from another activity (another .apk) at runtime in the separate Thread causes exception at this line Object o = c.newInstance(); which I came to know during debugging , otherwise class is Loaded nicely and also loaded method xyz() without any problem. But to execute that method I need to create Obect instance and then Method.invoke(o); I hope u got it now. Dianne:: Exception race is as follows, 04-08 11:57:08.802: ERROR/AndroidRuntime(240): Uncaught handler: thread Thread-8 exiting due to uncaught exception 04-08 11:57:08.812: ERROR/AndroidRuntime(240): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.os.Handler.init(Handler.java:111) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.app.Activity.init(Activity.java:660) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at test.check.Test1.init(Test1.java:6) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at java.lang.Class.newInstance(Native Method) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.giude.SocketFTP.SocketFTP.readTheFile(SocketFTP.java:128) 04-08 11:57:08.812: ERROR/AndroidRuntime(240): at android.giude.SocketFTP.SocketFTP$1.run(SocketFTP.java:50) Mariano : I have not used Looper.prepare(), Is it required?? If yes then where and how to define handler for that? On Apr 7, 10:20 pm, Dianne Hackborn hack...@android.com wrote: Also you need to look at the exception stack trace. It will show you who is trying to create a Handler. On Tue, Apr 7, 2009 at 9:44 AM, Streets Of Boston flyingdutc...@gmail.comwrote: But do you want a Looper and Handler in your thread 't'? Why do you load it in another thread? Are you planning in dispatching and handling messages in this thread 't'? If not, don't start adding all this stuff just to make the run-time happy. Put a breakpoint in your default constructor of your test.check.Test1 class and see which line of code needs a Handler or Looper. What are you trying to accomplish? It looks like you're trying to load and construct a class from an APK (check.apk). Are you initializing an Activity from this APK called 'Test1'? If so, I understand why it may need a looper/handler. But if you're loading an Activity (Test1) within another Activity (the one trying to load check.apk), you may get into a heap of other trouble. But i'm not an expert on this at all. Google engineers will know much more about this. On Apr 7, 10:55 am, Ask asifk1...@gmail.com wrote: Hi Streets, I dont think this is the case. Same code executes nicely If I call that method in the onCreate() method (i.e. main thread itself) problem occurs in the Object creation if I will call this method readTheFile() in the Thread. Can you give any info. regarding how I can define Looper and Handler in my code to
[android-developers] Re: Help...Exception in the Background Thread :Can't create handler inside thread that has not called Looper.prepare()
A view is bound to the thread it is created in. (Actually not view itself, but many subclasses have an internal Handler for scheduling work.) Yes, it can be annoying in some cases, but that's the way it is. On Wed, Apr 8, 2009 at 11:49 AM, Mariano Kamp mariano.k...@gmail.comwrote: Hi Dianne, what I am still wondering about is why I would need a Looper.prepare() to create a View object when it is not attached to a Window. Here is the background: I have a list activity and a show detail activity and an app. The user jumps frequently between the list activity and the show detail activity. In order to speed things up a bit I would like to cache the inflated GUI in app and call setContentView with this cached view in onCreate of show detail activity. And to make sure that also the first call to onCreate on show detail activity doesn't have to wait I would like to inflate it in a separate thread. On Wed, Apr 8, 2009 at 8:00 PM, Dianne Hackborn hack...@android.comwrote: Do it on the main thread. If you want to actually run UI on another thread, read up on Looper and Handler, do all the stuff such as calling Looper.prepare() in your thread, and then redesign your thread logic to be messages based on not do work more than a few seconds and all of the other constraints on a thread that is running UI. -- Dianne Hackborn Android framework engineer hack...@android.com Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-developers] Re: Help...Exception in the Background Thread :Can't create handler inside thread that has not called Looper.prepare()
Is there any handler needed for that?? Any Idea??? On Apr 7, 3:58 pm, Ask asifk1...@gmail.com wrote: Hi, I am using Reflection APIs in the background thread to call a method dynamically from another application but I am getting the Exception: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at a line : Object o = c.newInstance(); my code in the Thread is as follows, onCreate public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); hh_text = (TextView)findViewById(R.id.server_start); Thread t = new Thread() { public void run() { readTheFile(); } }; t.start(); } public void readTheFile() { try { PathClassLoader loader = new PathClassLoader( /sdcard/ test.check.apk, ClassLoader.getSystemClassLoader()); Class c = null; c = loader.loadClass(test.check.Test1); Log.i(See, Test Class Found ); Method method = c.getMethod(xyz, null); Object o = c.newInstance(); // This line throws exception String s = (String) method.invoke(o); Log.i(See,Got method: + s); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } Please help.. If you know the issue. On Apr 6, 7:14 pm, Ask asifk1...@gmail.com wrote: Thanx Mark for your quick reply. I will try it out and get back On Apr 6, 6:49 pm, Mark Murphy mmur...@commonsware.com wrote: Asif k wrote: But in my case, I am getting all 3 messages at a time after activity execution completed. That is probably because you are doing long-running work on the UI thread. Is there any API available, using which I can show the status during the execution also. Anything long-running should be in a background thread, in the activity or wrapped in a service. Then, use Handler or runOnUiThread() or something so the background thread can have the UI thread update your TextView. -- Mark Murphy (a Commons Guy)http://commonsware.com|http://twitter.com/commonsguy Android App Developer Training:http://commonsware.com/training.html-Hide quoted text - - Show quoted text -- Hide quoted text - - Show quoted text - --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-developers] Re: Help...Exception in the Background Thread :Can't create handler inside thread that has not called Looper.prepare()
It could be that during the construction of class test.check.Test1, a Handler or something similar (i.e. something needing the main-thread's message-queue) is constructed. Look at your test.check.Test1 implementation and see if this is the case. If this is the case, you will get an exception if the constructor of test.check.Test1 is executed on any other thread than the main-thread with the message queue. You would have gotten the same exception if you would have called 'new test.check.Test1()'. On Apr 7, 9:17 am, Ask asifk1...@gmail.com wrote: Is there any handler needed for that?? Any Idea??? On Apr 7, 3:58 pm, Ask asifk1...@gmail.com wrote: Hi, I am using Reflection APIs in the background thread to call a method dynamically from another application but I am getting the Exception: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at a line : Object o = c.newInstance(); my code in the Thread is as follows, onCreate public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); hh_text = (TextView)findViewById(R.id.server_start); Thread t = new Thread() { public void run() { readTheFile(); } }; t.start(); } public void readTheFile() { try { PathClassLoader loader = new PathClassLoader( /sdcard/ test.check.apk, ClassLoader.getSystemClassLoader()); Class c = null; c = loader.loadClass(test.check.Test1); Log.i(See, Test Class Found ); Method method = c.getMethod(xyz, null); Object o = c.newInstance(); // This line throws exception String s = (String) method.invoke(o); Log.i(See,Got method: + s); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } Please help.. If you know the issue. On Apr 6, 7:14 pm, Ask asifk1...@gmail.com wrote: Thanx Mark for your quick reply. I will try it out and get back On Apr 6, 6:49 pm, Mark Murphy mmur...@commonsware.com wrote: Asif k wrote: But in my case, I am getting all 3 messages at a time after activity execution completed. That is probably because you are doing long-running work on the UI thread. Is there any API available, using which I can show the status during the execution also. Anything long-running should be in a background thread, in the activity or wrapped in a service. Then, use Handler or runOnUiThread() or something so the background thread can have the UI thread update your TextView. -- Mark Murphy (a Commons Guy)http://commonsware.com|http://twitter.com/commonsguy Android App Developer Training:http://commonsware.com/training.html-Hidequoted text - - Show quoted text -- Hide quoted text - - Show quoted text -- Hide quoted text - - Show quoted text - --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-developers] Re: Help...Exception in the Background Thread :Can't create handler inside thread that has not called Looper.prepare()
Hi Streets, I dont think this is the case. Same code executes nicely If I call that method in the onCreate() method (i.e. main thread itself) problem occurs in the Object creation if I will call this method readTheFile() in the Thread. Can you give any info. regarding how I can define Looper and Handler in my code to execute it without any error.??? On Apr 7, 7:00 pm, Streets Of Boston flyingdutc...@gmail.com wrote: It could be that during the construction of class test.check.Test1, a Handler or something similar (i.e. something needing the main-thread's message-queue) is constructed. Look at your test.check.Test1 implementation and see if this is the case. If this is the case, you will get an exception if the constructor of test.check.Test1 is executed on any other thread than the main-thread with the message queue. You would have gotten the same exception if you would have called 'new test.check.Test1()'. On Apr 7, 9:17 am, Ask asifk1...@gmail.com wrote: Is there any handler needed for that?? Any Idea??? On Apr 7, 3:58 pm, Ask asifk1...@gmail.com wrote: Hi, I am using Reflection APIs in the background thread to call a method dynamically from another application but I am getting the Exception: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at a line : Object o = c.newInstance(); my code in the Thread is as follows, onCreate public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); hh_text = (TextView)findViewById(R.id.server_start); Thread t = new Thread() { public void run() { readTheFile(); } }; t.start(); } public void readTheFile() { try { PathClassLoader loader = new PathClassLoader( /sdcard/ test.check.apk, ClassLoader.getSystemClassLoader()); Class c = null; c = loader.loadClass(test.check.Test1); Log.i(See, Test Class Found ); Method method = c.getMethod(xyz, null); Object o = c.newInstance(); // This line throws exception String s = (String) method.invoke(o); Log.i(See,Got method: + s); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } Please help.. If you know the issue. On Apr 6, 7:14 pm, Ask asifk1...@gmail.com wrote: Thanx Mark for your quick reply. I will try it out and get back On Apr 6, 6:49 pm, Mark Murphy mmur...@commonsware.com wrote: Asif k wrote: But in my case, I am getting all 3 messages at a time after activity execution completed. That is probably because you are doing long-running work on the UI thread. Is there any API available, using which I can show the status during the execution also. Anything long-running should be in a background thread, in the activity or wrapped in a service. Then, use Handler or runOnUiThread() or something so the background thread can have the UI thread update your TextView. -- Mark Murphy (a Commons Guy)http://commonsware.com|http://twitter.com/commonsguy Android App Developer Training:http://commonsware.com/training.html-Hidequotedtext - - Show quoted text -- Hide quoted text - - Show quoted text -- Hide quoted text - - Show quoted text -- Hide quoted text - - Show quoted text - --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-developers] Re: Help...Exception in the Background Thread :Can't create handler inside thread that has not called Looper.prepare()
I don't want to sound like a smart ass, but did you try what the error message said? Called Looper.prepare()? I know I had a similar situation some time ago and it worked for me. Besides the dreaded BufferedReader exception I find the exceptions I get from the framework very helpful. On Tue, Apr 7, 2009 at 4:55 PM, Ask asifk1...@gmail.com wrote: Hi Streets, I dont think this is the case. Same code executes nicely If I call that method in the onCreate() method (i.e. main thread itself) problem occurs in the Object creation if I will call this method readTheFile() in the Thread. Can you give any info. regarding how I can define Looper and Handler in my code to execute it without any error.??? On Apr 7, 7:00 pm, Streets Of Boston flyingdutc...@gmail.com wrote: It could be that during the construction of class test.check.Test1, a Handler or something similar (i.e. something needing the main-thread's message-queue) is constructed. Look at your test.check.Test1 implementation and see if this is the case. If this is the case, you will get an exception if the constructor of test.check.Test1 is executed on any other thread than the main-thread with the message queue. You would have gotten the same exception if you would have called 'new test.check.Test1()'. On Apr 7, 9:17 am, Ask asifk1...@gmail.com wrote: Is there any handler needed for that?? Any Idea??? On Apr 7, 3:58 pm, Ask asifk1...@gmail.com wrote: Hi, I am using Reflection APIs in the background thread to call a method dynamically from another application but I am getting the Exception: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at a line : Object o = c.newInstance(); my code in the Thread is as follows, onCreate public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); hh_text = (TextView)findViewById(R.id.server_start); Thread t = new Thread() { public void run() { readTheFile(); } }; t.start(); } public void readTheFile() { try { PathClassLoader loader = new PathClassLoader( /sdcard/ test.check.apk, ClassLoader.getSystemClassLoader()); Class c = null; c = loader.loadClass(test.check.Test1); Log.i(See, Test Class Found ); Method method = c.getMethod(xyz, null); Object o = c.newInstance(); // This line throws exception String s = (String) method.invoke(o); Log.i(See,Got method: + s); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } Please help.. If you know the issue. On Apr 6, 7:14 pm, Ask asifk1...@gmail.com wrote: Thanx Mark for your quick reply. I will try it out and get back On Apr 6, 6:49 pm, Mark Murphy mmur...@commonsware.com wrote: Asif k wrote: But in my case, I am getting all 3 messages at a time after activity execution completed. That is probably because you are doing long-running work on the UI thread. Is there any API available, using which I can show the status during the execution also. Anything long-running should be in a background thread, in the activity or wrapped in a service. Then, use Handler or runOnUiThread() or something so the background thread can have the UI thread update your TextView. -- Mark Murphy (a Commons Guy)http://commonsware.com| http://twitter.com/commonsguy Android App Developer Training: http://commonsware.com/training.html-Hidequotedtext - - Show quoted text -- Hide quoted text - - Show quoted text -- Hide quoted text - - Show quoted text -- Hide quoted text - - Show quoted text - --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-developers] Re: Help...Exception in the Background Thread :Can't create handler inside thread that has not called Looper.prepare()
But do you want a Looper and Handler in your thread 't'? Why do you load it in another thread? Are you planning in dispatching and handling messages in this thread 't'? If not, don't start adding all this stuff just to make the run-time happy. Put a breakpoint in your default constructor of your test.check.Test1 class and see which line of code needs a Handler or Looper. What are you trying to accomplish? It looks like you're trying to load and construct a class from an APK (check.apk). Are you initializing an Activity from this APK called 'Test1'? If so, I understand why it may need a looper/handler. But if you're loading an Activity (Test1) within another Activity (the one trying to load check.apk), you may get into a heap of other trouble. But i'm not an expert on this at all. Google engineers will know much more about this. On Apr 7, 10:55 am, Ask asifk1...@gmail.com wrote: Hi Streets, I dont think this is the case. Same code executes nicely If I call that method in the onCreate() method (i.e. main thread itself) problem occurs in the Object creation if I will call this method readTheFile() in the Thread. Can you give any info. regarding how I can define Looper and Handler in my code to execute it without any error.??? On Apr 7, 7:00 pm, Streets Of Boston flyingdutc...@gmail.com wrote: It could be that during the construction of class test.check.Test1, a Handler or something similar (i.e. something needing the main-thread's message-queue) is constructed. Look at your test.check.Test1 implementation and see if this is the case. If this is the case, you will get an exception if the constructor of test.check.Test1 is executed on any other thread than the main-thread with the message queue. You would have gotten the same exception if you would have called 'new test.check.Test1()'. On Apr 7, 9:17 am, Ask asifk1...@gmail.com wrote: Is there any handler needed for that?? Any Idea??? On Apr 7, 3:58 pm, Ask asifk1...@gmail.com wrote: Hi, I am using Reflection APIs in the background thread to call a method dynamically from another application but I am getting the Exception: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at a line : Object o = c.newInstance(); my code in the Thread is as follows, onCreate public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); hh_text = (TextView)findViewById(R.id.server_start); Thread t = new Thread() { public void run() { readTheFile(); } }; t.start(); } public void readTheFile() { try { PathClassLoader loader = new PathClassLoader( /sdcard/ test.check.apk, ClassLoader.getSystemClassLoader()); Class c = null; c = loader.loadClass(test.check.Test1); Log.i(See, Test Class Found ); Method method = c.getMethod(xyz, null); Object o = c.newInstance(); // This line throws exception String s = (String) method.invoke(o); Log.i(See,Got method: + s); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } Please help.. If you know the issue. On Apr 6, 7:14 pm, Ask asifk1...@gmail.com wrote: Thanx Mark for your quick reply. I will try it out and get back On Apr 6, 6:49 pm, Mark Murphy mmur...@commonsware.com wrote: Asif k wrote: But in my case, I am getting all 3 messages at a time after activity execution completed. That is probably because you are doing long-running work on the UI thread. Is there any API available, using which I can show the status during the execution also. Anything long-running should be in a background thread, in the activity or wrapped in a service. Then, use Handler or runOnUiThread() or something so the background thread can have the UI thread update your TextView. -- Mark Murphy (a Commons Guy)http://commonsware.com|http://twitter.com/commonsguy Android App Developer Training:http://commonsware.com/training.html-Hidequotedtext- - Show quoted text -- Hide quoted text - - Show quoted text -- Hide quoted text - - Show quoted text -- Hide quoted text - - Show quoted text -- Hide quoted text - - Show quoted text - --~--~-~--~~~---~--~~ 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] Re: Help...Exception in the Background Thread :Can't create handler inside thread that has not called Looper.prepare()
Also you need to look at the exception stack trace. It will show you who is trying to create a Handler. On Tue, Apr 7, 2009 at 9:44 AM, Streets Of Boston flyingdutc...@gmail.comwrote: But do you want a Looper and Handler in your thread 't'? Why do you load it in another thread? Are you planning in dispatching and handling messages in this thread 't'? If not, don't start adding all this stuff just to make the run-time happy. Put a breakpoint in your default constructor of your test.check.Test1 class and see which line of code needs a Handler or Looper. What are you trying to accomplish? It looks like you're trying to load and construct a class from an APK (check.apk). Are you initializing an Activity from this APK called 'Test1'? If so, I understand why it may need a looper/handler. But if you're loading an Activity (Test1) within another Activity (the one trying to load check.apk), you may get into a heap of other trouble. But i'm not an expert on this at all. Google engineers will know much more about this. On Apr 7, 10:55 am, Ask asifk1...@gmail.com wrote: Hi Streets, I dont think this is the case. Same code executes nicely If I call that method in the onCreate() method (i.e. main thread itself) problem occurs in the Object creation if I will call this method readTheFile() in the Thread. Can you give any info. regarding how I can define Looper and Handler in my code to execute it without any error.??? On Apr 7, 7:00 pm, Streets Of Boston flyingdutc...@gmail.com wrote: It could be that during the construction of class test.check.Test1, a Handler or something similar (i.e. something needing the main-thread's message-queue) is constructed. Look at your test.check.Test1 implementation and see if this is the case. If this is the case, you will get an exception if the constructor of test.check.Test1 is executed on any other thread than the main-thread with the message queue. You would have gotten the same exception if you would have called 'new test.check.Test1()'. On Apr 7, 9:17 am, Ask asifk1...@gmail.com wrote: Is there any handler needed for that?? Any Idea??? On Apr 7, 3:58 pm, Ask asifk1...@gmail.com wrote: Hi, I am using Reflection APIs in the background thread to call a method dynamically from another application but I am getting the Exception: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at a line : Object o = c.newInstance(); my code in the Thread is as follows, onCreate public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); hh_text = (TextView)findViewById(R.id.server_start); Thread t = new Thread() { public void run() { readTheFile(); } }; t.start(); } public void readTheFile() { try { PathClassLoader loader = new PathClassLoader( /sdcard/ test.check.apk, ClassLoader.getSystemClassLoader()); Class c = null; c = loader.loadClass(test.check.Test1); Log.i(See, Test Class Found ); Method method = c.getMethod(xyz, null); Object o = c.newInstance(); // This line throws exception String s = (String) method.invoke(o); Log.i(See,Got method: + s); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } Please help.. If you know the issue. On Apr 6, 7:14 pm, Ask asifk1...@gmail.com wrote: Thanx Mark for your quick reply. I will try it out and get back On Apr 6, 6:49 pm, Mark Murphy mmur...@commonsware.com wrote: Asif k wrote: But in my case, I am getting all 3 messages at a time after activity execution completed. That is probably because you are doing long-running work on the UI thread. Is there any API available, using which I can show the status during the execution also. Anything long-running should be in a background thread, in the activity or wrapped in a service. Then, use Handler or runOnUiThread() or something so the background thread can have the UI thread update your TextView. -- Mark Murphy (a Commons Guy)http://commonsware.com| http://twitter.com/commonsguy Android App Developer Training: http://commonsware.com/training.html-Hidequotedtext- - Show quoted text -- Hide quoted text - - Show quoted text -- Hide quoted text - -