0

I'm making a simple app which plays 2 quick mp3 files a few seconds apart. I'm using a thread for timing

Within the thread, I do a setText to the xml file called myquick and it works great, but after the 10 second wait, when the next mp3 file is called, the next setText doesn't work and forces a close. When I comment out the problematic code...

// mytxt.setText("We will now end");

the whole thread goes through its paces and then happily goes back to the listview menu.

Question... how can I get the text to change on the screen? How can I make this work?

Here are my 2 related files

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" 

  >

<TextView
    android:id="@+id/ses1text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_gravity="center"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:text=" "
    android:textSize="25dp" >
</TextView>

</RelativeLayout>

And here is my JAVA Code which has the issue

public class MyQuick extends Activity {

MediaPlayer mp;
Thread threetimer;


@Override
public void onCreate(Bundle twothings) {
    // TODO Auto-generated method stub
    super.onCreate(twothings);

    setContentView(R.layout.myquick);
    threetimer = new Thread(){

        public void run(){
            try{
             TextView mytxt = (TextView) findViewById(R.id.ses1text);
                mp = MediaPlayer.create(MyQuick.this, R.raw.start);

                mytxt.setText("Let's start in 10 seconds");
                // it works here 

                mp.start();
                sleep(5000);
                mp.release();
                sleep(10000);

 // HERE IS THE PROBLEM

                mp = MediaPlayer.create(MyQuick.this, R.raw.end);

                mytxt.setText("We will now end");
                //it doesn't work here, 
                                    //it closes this portion of the app and goes back to the listview 
                //needless to say the text does not change at all
                mp.start();
                sleep(5000);
                mp.release();


            }catch (InterruptedException e){
                e.printStackTrace();


            }finally{

                Intent myIntent = new Intent("com.my.quick.MENU");
                startActivity(myIntent);




            }
        }

    };

        threetimer.start();

        }
        }
CodeSimeon5
  • 11
  • 1
  • 5
  • 1
    The stack trace of the error is??? – Adel Boutros Jul 11 '13 at 20:50
  • You can't change UI widgets in a simple Thread. Read this http://stackoverflow.com/questions/11140285/how-to-use-runonuithread or use AsyncTask class and put your UI changes in `onProgressUpdate` method. – Cob013 Jul 11 '13 at 20:51
  • Rob013 answered this question in less than 30 seconds... WOW! I am beyond impressed. I went to the link suggested, implemented the code and now my text changes Thank you! – CodeSimeon5 Jul 11 '13 at 21:10

0 Answers0