Dan U, you are right. It's the Toast which needs to be executed by an
UI handler(created in UI thread).

I made a Toast notification after I caught the InterruptedException,
but I forgot I was in a non-UI thread.
What a stupid mistake....

The solution is easy:
                         put the toast notification in a separate
runnable task
                         and post it to the UI thread with a handler created in 
UI thread

Here in the class LoadingTask2:

class LoadingTask2 implements Runnable{
                ...
                ...
                @Override
                public void run() {
                        try {
                                Thread.sleep(10000);
                                
remoteHandler.post(AsyncLoadingMgrSample.this.finishLoadingTask);
                        } catch (InterruptedException e) {
                                /*
                                 * Exception is thrown because:
                                 *
                                 * Toast has to be shown by an UI thread, or 
non-UI thread can't
create a toast
                                 */
//                              
Toast.makeText(AsyncLoadingMgrSample.this,"loading task
error",Toast.LENGTH_LONG).show();

                                /*
                                 * put the toast notification in a separate 
runnable task
                                 * and post it to the UI thread with a handler 
created in UI
thread
                                 */
                                
remoteHandler.post(AsyncLoadingMgrSample.this.cancelLodingTask);

                                Log.i("LoadingTask2:Run","loading task 
interrupted");
                        }

                }

On Apr 2, 4:00 am, "Dan U." <[EMAIL PROTECTED]> wrote:
> That doesn't seem to have much useful info. The original message looks
> like it's related to the handler and the ui thread. I'm not entirely
> sure what is happening. It almost seems like you are constructing a
> Handler from a non-ui thread, but I didn't see where you do that in
> the code.
>
> On Apr 1, 12:55 am, Rader <[EMAIL PROTECTED]> wrote:
>
> > Here is the log after I click the cancel button:
>
> > DEBUG/dalvikvm(1299): Exception Ljava/lang/RuntimeException; from
> > Handler.java:94 not caught locally
> > WARN/dalvikvm(1299): threadid=15: thread exiting with uncaught
> > exception (group=0x4000fdf8)
> > ERROR/AndroidRuntime(1299): Uncaught handler: thread Thread-8 exiting
> > due to uncaught exception
> > ERROR/AndroidRuntime(1299): java.lang.RuntimeException: Can't create
> > handler inside thread that has not called Looper.prepare()
> > ERROR/AndroidRuntime(1299):     at
> > android.os.Handler.<init>(Handler.java:94)
> > ERROR/AndroidRuntime(1299):     at
> > android.widget.Toast.<init>(Toast.java:348)
> > ERROR/AndroidRuntime(1299):     at
> > android.widget.Toast.makeText(Toast.java:209)
> > ERROR/AndroidRuntime(1299):     at ciente.cl.AsyncLoadingMgrSample
> > $LoadingTask2.run(AsyncLoadingMgrSample.java:146)
> > ERROR/AndroidRuntime(1299):     at java.lang.Thread.run(Thread.java:
> > 896)
> > INFO/Process(508): Sending signal. PID: 1299 SIG: 3
> > INFO/dalvikvm(1299): threadid=7: reacting to signal 3
> > INFO/dalvikvm(1299): DALVIK THREADS:
> > INFO/dalvikvm(1299): "Main" prio=5 tid=3 RUNNABLE
> > INFO/dalvikvm(1299):   | group="main" sCount=1 dsCount=0 s=1
> > obj=0x40010450
> > INFO/dalvikvm(1299):   | sysTid=496 nice=0 sched=0/0
> > handle=-1096934148
> > INFO/dalvikvm(1299):   at java.lang.Thread.interrupt(Thread.java:~747)
> > INFO/dalvikvm(1299):   at ciente.cl.AsyncLoadingMgrSample
> > $1$1.onCancel(AsyncLoadingMgrSample.java:72)
> > INFO/dalvikvm(1299):   at android.app.Dialog
> > $1.handleMessage(Dialog.java:508)
> > INFO/dalvikvm(1299):   at
> > android.os.Handler.dispatchMessage(Handler.java:80)
> > INFO/dalvikvm(1299):   at android.os.Looper.loop(Looper.java:91)
> > INFO/dalvikvm(1299):   at
> > android.app.ActivityThread.main(ActivityThread.java:3052)
> > INFO/dalvikvm(1299):   (break frame)
> > INFO/dalvikvm(1299):   at java.lang.reflect.Method.invokeNative(Native
> > Method)
> > INFO/dalvikvm(1299):   at java.lang.reflect.Method.invoke(Method.java:
> > 356)
> > INFO/dalvikvm(1299):   at android.os.ZygoteInit
> > $MethodAndArgsCaller.run(ZygoteInit.java:1547)
> > INFO/dalvikvm(1299):   at android.os.ZygoteInit.main(ZygoteInit.java:
> > 1445)
> > INFO/dalvikvm(1299):   (break frame)
> > INFO/dalvikvm(1299):   at android.dalvik.NativeStart.main(Native
> > Method)
> > INFO/dalvikvm(1299):   (break frame)
> > INFO/dalvikvm(1299): "Thread-8" prio=5 tid=15 NATIVE
> > INFO/dalvikvm(1299):   | group="main" sCount=1 dsCount=0 s=0
> > obj=0x40060a90
> > INFO/dalvikvm(1299):   | sysTid=1357 nice=0 sched=0/0 handle=1346432
> > INFO/dalvikvm(1299):   at android.os.BinderProxy.transact(Native
> > Method)
> > INFO/dalvikvm(1299):   at
> > android.app.ActivityManagerProxy.handleApplicationError(ActivityManagerNative.java:
> > 1441)
> > INFO/dalvikvm(1299):   at
> > android.os.RuntimeInit.crash(RuntimeInit.java:250)
> > INFO/dalvikvm(1299):   at android.os.RuntimeInit
> > $UncaughtHandler.uncaughtException(RuntimeInit.java:56)
> > INFO/dalvikvm(1299):   at
> > java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:853)
> > INFO/dalvikvm(1299):   at
> > java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:850)
> > INFO/dalvikvm(1299):   (break frame)
> > INFO/dalvikvm(1299): "Binder Thread #2" prio=5 tid=13 NATIVE
> > INFO/dalvikvm(1299):   | group="main" sCount=1 dsCount=0 s=0
> > obj=0x40021c38
> > INFO/dalvikvm(1299):   | sysTid=1312 nice=-10 sched=0/0 handle=1320328
> > INFO/dalvikvm(1299):   at android.dalvik.NativeStart.run(Native
> > Method)
> > INFO/dalvikvm(1299):   (break frame)
> > INFO/dalvikvm(1299): "Binder Thread #1" prio=5 tid=11 NATIVE
> > INFO/dalvikvm(1299):   | group="main" sCount=1 dsCount=0 s=0
> > obj=0x40020438
> > INFO/dalvikvm(1299):   | sysTid=1311 nice=-15 sched=0/0 handle=1317024
> > INFO/dalvikvm(1299):   at android.dalvik.NativeStart.run(Native
> > Method)
> > INFO/dalvikvm(1299):   (break frame)
> > INFO/dalvikvm(1299): "JDWP" daemon prio=5 tid=9 VMWAIT
> > INFO/dalvikvm(1299):   | group="system" sCount=1 dsCount=0 s=0
> > obj=0x400b4530
> > INFO/dalvikvm(1299):   | sysTid=1302 nice=0 sched=0/0 handle=1313496
> > INFO/dalvikvm(1299):   at android.dalvik.NativeStart.run(Native
> > Method)
> > INFO/dalvikvm(1299):   (break frame)
> > INFO/dalvikvm(1299): "Signal Catcher" daemon prio=5 tid=7 RUNNABLE
> > INFO/dalvikvm(1299):   | group="system" sCount=0 dsCount=0 s=0
> > obj=0x400b1e90
> > INFO/dalvikvm(1299):   | sysTid=1301 nice=0 sched=0/0 handle=1302384
> > INFO/dalvikvm(1299):   at android.dalvik.NativeStart.run(Native
> > Method)
> > INFO/dalvikvm(1299):   (break frame)
> > INFO/dalvikvm(1299): "HeapWorker" daemon prio=5 tid=5 VMWAIT
> > INFO/dalvikvm(1299):   | group="system" sCount=1 dsCount=0 s=0
> > obj=0x400b0670
> > INFO/dalvikvm(1299):   | sysTid=1300 nice=0 sched=0/0 handle=1299784
> > INFO/dalvikvm(1299):   at android.dalvik.NativeStart.run(Native
> > Method)
> > INFO/dalvikvm(1299):   (break frame)
> > INFO/CancelLoadingTask:Run(1299): loading task interruptted
> > VERBOSE/Toast(1299): SHOW: [EMAIL PROTECTED]
> > VERBOSE/Toast(1299): HANDLE SHOW: [EMAIL PROTECTED]
> > showing=false
> > VERBOSE/Toast(1299): ADD! [EMAIL PROTECTED] in
> > [EMAIL PROTECTED]
> > VERBOSE/Toast(1299): HIDE: [EMAIL PROTECTED]
> > VERBOSE/Toast(1299): HANDLE HIDE: [EMAIL PROTECTED]
> > showing=true
> > VERBOSE/Toast(1299): REMOVE! [EMAIL PROTECTED] in
> > [EMAIL PROTECTED]
>
> > On Apr 1, 3:02 pm, "Dan U." <[EMAIL PROTECTED]> wrote:
>
> > > Do you have a line number for this exception (Should be in logcat).
> > > I'd guess maybe it's the Toast show() call needing to be executed in a
> > > handler.
>
> > > On Mar 31, 11:15 pm, Rader <[EMAIL PROTECTED]> wrote:
>
> > > > What i want to do is starting a new thread when i click a button and
> > > > this new thread will do some long-time tasks. A loading dialog will be
> > > > shown before that thread starts and will be hidden when the thread
> > > > ends.If i wait until the thread finishes its job and ends normally,
> > > > everything will be OK. But if i cancel the loading dialog and
> > > > "interrupt" the thread in the cancel event handler of that dialog, an
> > > > UnCaughtExcetpion will be thrown.
>
> > > > Quote:
> > > > Can't create handler inside the thread that has not called
> > > > Looper.prepare()
>
> > > > Full code: AsyncLoadingMgrSample .java
>
> > > > Java:
>
> > > > package ciente.cl;
>
> > > > import java.util.concurrent.Callable;
> > > > import java.util.concurrent.CompletionService;
> > > > import java.util.concurrent.Executor;
> > > > import java.util.concurrent.ExecutorCompletionService;
> > > > import java.util.concurrent.FutureTask;
>
> > > > import android.app.Activity;
> > > > import android.app.ProgressDialog;
> > > > import android.content.DialogInterface;
> > > > import android.content.DialogInterface.OnCancelListener;
> > > > import android.os.Bundle;
> > > > import android.os.Handler;
> > > > import android.util.Log;
> > > > import android.view.View;
> > > > import android.view.View.OnClickListener;
> > > > import android.widget.Button;
> > > > import android.widget.EditText;
> > > > import android.widget.Toast;
>
> > > > /**
> > > >  * @author rader
> > > >  *
> > > >  */
> > > > public class AsyncLoadingMgrSample extends Activity {
>
> > > >      private final AsyncLoadingMgr mgr=new AsyncLoadingMgr(this);
> > > >      protected Runnable cancelLoding;
> > > >      protected FutureTask<?> loading;;
> > > >      ProgressDialog mProgDlg;
>
> > > >      final public FinishlLoadingTask finishLoadingTask=new
> > > > FinishlLoadingTask();
>
> > > >      /** Called when the activity is first created. */
> > > >      @Override
> > > >      public void onCreate(Bundle icicle) {
> > > >           super.onCreate(icicle);
> > > >           setContentView(R.layout.asyncloadingmgrsample);
>
> > > > //        loading=new FutureTask<String>(new LoadingTask(this));
> > > > //        cancelLoding=new CancelLoadingTask();
>
> > > >           final Handler handler=new Handler();
>
> > > >           Button btnAsyncLoad=(Button)findViewById(R.id.btnAsyncLoad);
> > > >           btnAsyncLoad.setOnClickListener(new OnClickListener(){
>
> > > >                @Override
> > > >                public void onClick(View arg0){
> > > >                     EditText
> > > > txt=(EditText)findViewById(R.id.txtAsyncLoad);
>
> > > >  //                    Executor
> > > > executor=java.util.concurrent.Executors.newFixedThreadPool(1);
> > > >  //                    CompletionService<String> completionService =
> > > >  //                       new
> > > > ExecutorCompletionService<String>(executor);
> > > >                     txt.setText("start task...");
> > > >                     try {
> > > > //                       Callable task=new LoadingTask(handler);
> > > >                          final Thread loadingThread=new Thread(new
> > > > LoadingTask2(handler));
> > > >                          loadingThread.start();
>
> > > >                          mProgDlg =
> > > > ProgressDialog.show(AsyncLoadingMgrSample.this, null,
> > > >                                    "Please wait while loading...",
> > > > true, true);
> > > >                          mProgDlg.setCancelListener(new
> > > > OnCancelListener(){
>
> > > >                               @Override
> > > >                               public void onCancel(DialogInterface
> > > > arg0) {
> > > >                                    loadingThread.interrupt();
>
> > > > //                                 while(!
> > > > loadingThread.isInterrupted()){
> > > > //
> > > > //                                 }
>
> ...
>
> read more ยป
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to