problem with thread in Android

Put your problem here if it does not fit any of the other categories.

problem with thread in Android

Postby Gelassen » Mon Nov 15, 2010 8:14 am

Good day.
I have a problem with simple thread in Android.
Firstly, In my class which extends activity and shows first form, I have a field - class whisch extends Thread. Next, in method "onCreate" I created button and added it "onClickListener"
Secondly, in this event handler I create new thread and start it. But in this moment my application falls/ I don't know what's happend. What did I do wrong?
Here my code without another method:
StartActivity.java
Code: Select all

public class StartActivity extends Activity {

   private static Bundle mBundle;
   private Context mContext = this;
   private Connection mThread;
   private static final int SHOW_SUBACTIVITY = 1;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      
      Button btEnter = (Button) findViewById(R.id.enter);
      btEnter.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {
            
                 Handler handler = new Handler();
            mThread = new Connection(mBundle, handler);
            mThread.start();
            
         }
      });

      Button btCancel = (Button) findViewById(R.id.cancel);
      btCancel.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
         }
      });
   }
}

And Connection.java
Code: Select all

public final class Connection extends Thread {

   private static String myString;
   private static Handler mHandler;
   private static Bundle mBundle;

   /*
    * (non-Javadoc)
    *
    * @see java.lang.Thread#run()
    */
   @Override
   public void run() {
      // TODO Auto-generated method stub
      super.run();
      try {

      } catch (Exception e) {

      }
   }
   public Connection(Bundle bundle, Handler handler) {
      // TODO Auto-generated constructor stub
      mBundle = bundle;
      mHandler = handler;
   }
}
Gelassen
Freshman
Freshman
 
Posts: 6
Joined: Wed Nov 03, 2010 10:56 am

Top

Re: problem with thread in Android

Postby urbantrad » Mon Nov 15, 2010 1:38 pm

"my application falls"
What do you mean by that?
From what i can tell looking at the code, nothing will happen since there's nothing you do in the run() method. It will simply end, ending the thread as well. Also, calling super.run() seems suspicious, i didn't know threads had default behavior...
urbantrad
Senior Developer
Senior Developer
 
Posts: 104
Joined: Thu Sep 09, 2010 10:19 pm

Re: problem with thread in Android

Postby Gelassen » Tue Nov 16, 2010 12:07 am

When I had restart system all run coorect/ I don't know what happend but problem had solved. Thaks :)
Gelassen
Freshman
Freshman
 
Posts: 6
Joined: Wed Nov 03, 2010 10:56 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 13 guests