"Try to remove "synchronized" from stopThread() and startThread() "

this worked, thanks!

On Jul 20, 7:52 pm, vladimir kroz <[email protected]> wrote:
> Try to remove "synchronized" from stopThread() and startThread()
>
> On Jul 17, 6:29 pm, Kevin Grant <[email protected]> wrote:
>
>
>
> > I understand the concept of threads, and it almost seems like I
> > understand how to implement them.  This code executes properly until I
> > press the back button (to close), and then gives a "This application
> > has stopped unexpectedly.  Please try again."  Well duh, I closed it!
>
> > Heres the code Im using:
>
> > (It is a slightly modified version of the example provided
> > here:http://www.helloandroid.com/node/243
>
> > using the modifications I found
> > here:http://stackoverflow.com/questions/680180/where-to-stop-destroy-threa...
> > )
>
> > The import Pi class comes from this website, its just an arbitrary
> > class that will perform a long calculation to show the example
>
> >http://blog.taragana.com/index.php/archive/calculate-pi-to-arbitrary-...
>
> > /*************************************************/
> > package com.example;
>
> > import com.example.Pi;
> > import android.app.Activity;
> > import android.app.ProgressDialog;
> > 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;
>
> > public class AndroidThreads extends Activity implements Runnable {
>
> >     private String pi_string;
> >     private TextView tv;
> >     private ProgressDialog pd;
> >     private volatile Thread runner;
>
> >     @Override
> >     public void onCreate(Bundle icicle) {
> >         super.onCreate(icicle);
> >         setContentView(R.layout.main);
>
> >         mp = MediaPlayer.create(getBaseContext(),
> > R.raw.camera_click); //this is using the slow.mp3 resource
> >         tv = (TextView) this.findViewById(R.id.box);
> >         tv.setText("Press submit to start calculation");
>
> >                 Button submit = (Button) findViewById(R.id.submit_btn);
> >         submit.setOnClickListener(new OnClickListener(){
> >                 public void onClick(View v){
> >                         startThread();
> >                 }
> >         });
> >     }
>
> >     public synchronized void startThread(){
> >       if(runner == null){
> >         pd = ProgressDialog.show(this, "Working..", "Calculating Pi",
> > true, false);
> >         runner = new Thread(this);
> >         runner.start();
> >       }
> >     }
>
> >     public synchronized void stopThread(){
> >       if(runner != null){
> >         Thread moribund = runner;
> >         runner = null;
> >         moribund.interrupt();
> >       }
> >     }
>
> >     public void run(){
> >           pi_string = Pi.computePi(800).toString();
> >           handler.sendEmptyMessage(0);
> >     }
>
> >     private Handler handler = new Handler() {
> >         @Override
> >         public void handleMessage(Message msg) {
> >             pd.dismiss();
> >             tv.setText(pi_string);
> >         }
> >     };}
>
> > /*************************************************/
>
> > So like I said, this does everything I want it to do, except... it
> > gives the crash error when I exit the program.
>
> > *So what I need to know is why is it crashing, and how can I fix it?*
>
> > It seems to me like the threads aren't all finishing properly, so I
> > tried putting the stopThread() function into the onStop() and onDestroy
> > () overrides, but that didn't seem to make a difference.  Appreciate
> > any input!
>
> > -kevin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to