Wednesday, February 2, 2011

Re: [android-developers] Problem in Handler

You are sending the same messages into the handler while they are still being dispatched.  Each message you send needs to be a new one you obtain.

On Wed, Feb 2, 2011 at 8:16 PM, yogi <yogi.rulzz@gmail.com> wrote:
Hi all,
I have created to thread in an application, and from these threads i
am sending a value.
But the App has started but after 5-6 seconds it crashes and with the
following log error.


02-03 09:40:19.598: ERROR/AndroidRuntime(1684): FATAL EXCEPTION: main
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):
android.util.AndroidRuntimeException: { what=1000 when=2469427 obj=4 }
This message is already in use.
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.os.MessageQueue.enqueueMessage(MessageQueue.java:171)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.os.Handler.sendMessageAtTime(Handler.java:457)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.os.Handler.sendMessageDelayed(Handler.java:430)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.os.Handler.sendEmptyMessageDelayed(Handler.java:394)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.os.Handler.sendEmptyMessage(Handler.java:379)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.view.ViewRoot.scheduleTraversals(ViewRoot.java:652)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.view.ViewRoot.requestLayout(ViewRoot.java:596)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.view.View.requestLayout(View.java:8125)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.view.View.requestLayout(View.java:8125)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.view.View.requestLayout(View.java:8125)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.view.View.requestLayout(View.java:8125)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.view.View.requestLayout(View.java:8125)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.widget.TextView.checkForRelayout(TextView.java:5378)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.widget.TextView.setText(TextView.java:2688)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.widget.TextView.setText(TextView.java:2556)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.widget.TextView.setText(TextView.java:2531)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
com.demo.app.handlerapp$3.handleMessage(handlerapp.java:95)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.os.Handler.dispatchMessage(Handler.java:99)

Please refer to the following code which i have written.

package com.demo.app;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class handlerapp extends Activity implements OnClickListener{
   /** Called when the activity is first created. */
       @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       button=(Button)findViewById(R.id.Button01);
       tw=(TextView)findViewById(R.id.TextView01);
       tw.setText("click button to start");
       button.setOnClickListener(this);
  }
       @Override
       public void onClick(View v) {
               // TODO Auto-generated method stub
       switch(v.getId()){
       case R.id.Button01:
               Toast.makeText(getApplicationContext(), "clicked", 2).show();
               if(!th.isAlive())
           th.start();
               if(!th1.isAlive())
                       th1.start();
               break;
       }

       }
       Thread th= new Thread(new Runnable(){

               @Override
               public void run() {
                       // TODO Auto-generated method stub
                       Message msg= handler.obtainMessage();
                       for(int i=2;i<1000;i++)
                       {
                               try {
                                       Thread.sleep(5000);
                               } catch (InterruptedException e) {
                                       // TODO Auto-generated catch block
                                       e.printStackTrace();
                               }
                       msg.obj= i;
                       msg.what=2;
                       handler.sendMessage(msg);
                       }

               }

       });
       Thread th1= new Thread(new Runnable(){
               @Override
               public void run() {
                       // TODO Auto-generated method stub
                       Message msg= handler.obtainMessage();
                       for(int i=2;i<1000;i++)
                       {
                               try {
                                       Thread.sleep(3000);
                               } catch (InterruptedException e) {
                                       // TODO Auto-generated catch block
                                       e.printStackTrace();
                               }
                       msg.obj= i;
                       msg.what=1;
                       handler.sendMessage(msg);
                       }

               }

       });

       Handler handler = new Handler(){
               @Override
               public void handleMessage(Message msg) {
                       // TODO Auto-generated method stub
                       super.handleMessage(msg);
                       switch(msg.what){
                       case 1:
                               Toast.makeText(getApplicationContext(), msg.obj.toString(),
1).show();
                               break;
                       case 2:

                               tw.setText(msg.obj.toString());
                               removeMessages(msg.what, msg.obj);
                       break;
                       }
               }

   };


       Button button;
       TextView tw;
}



Thanks in Advance
Regards
Yogi

--
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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--
Dianne Hackborn
Android framework engineer
hackbod@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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment