-1

i am trying to implement Runnable and run the Run() method when a thread is started. but when i run the program it crashed.

MainActivity

public class MainActivity extends Activity implements Runnable{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Thread t1;
            t1=new Thread(this);
            t1.start();
        }

        public void run() {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this, "display something",
                    Toast.LENGTH_LONG).show();
        }

i tried changing it to t1=new Thread(new MainActivity());(app crashed) or just t1=new Thread(); never crash but no output.

how do i implement a Runnable Run when a thread is started? i search all over the place but could not find an answer. i need to include this function in my main project code too. but i create a separate testing project just to get how this works so i can add it in my main project code myself. at my main project it crashed at this point too. it never reached the Run method.

after it crashed, this is the LogCat

01-21 13:03:06.460: W/dalvikvm(879): threadid=11: thread exiting with uncaught exception (group=0xb3a6fb90)
01-21 13:03:06.460: E/AndroidRuntime(879): FATAL EXCEPTION: Thread-51
01-21 13:03:06.460: E/AndroidRuntime(879): Process: com.example.testthreadrun, PID: 879
01-21 13:03:06.460: E/AndroidRuntime(879): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
01-21 13:03:06.460: E/AndroidRuntime(879):  at android.os.Handler.<init>(Handler.java:200)
01-21 13:03:06.460: E/AndroidRuntime(879):  at android.os.Handler.<init>(Handler.java:114)
01-21 13:03:06.460: E/AndroidRuntime(879):  at android.widget.Toast$TN.<init>(Toast.java:327)
01-21 13:03:06.460: E/AndroidRuntime(879):  at android.widget.Toast.<init>(Toast.java:92)
01-21 13:03:06.460: E/AndroidRuntime(879):  at android.widget.Toast.makeText(Toast.java:241)
01-21 13:03:06.460: E/AndroidRuntime(879):  at com.example.testthreadrun.MainActivity.run(MainActivity.java:29)
01-21 13:03:06.460: E/AndroidRuntime(879):  at java.lang.Thread.run(Thread.java:841)
01-21 13:03:07.010: I/Choreographer(879): Skipped 126 frames!  The application may be doing too much work on its main thread.
01-21 13:03:07.970: I/Choreographer(879): Skipped 165 frames!  The application may be doing too much work on its main thread.
01-21 13:03:08.840: D/gralloc_goldfish(879): Emulator without GPU emulation detected.
01-21 13:03:10.770: I/Choreographer(879): Skipped 31 frames!  The application may be doing too much work on its main thread.
01-21 13:03:26.670: I/Process(879): Sending signal. PID: 879 SIG: 9
Nambi
  • 11,944
  • 3
  • 37
  • 49
Myst
  • 177
  • 1
  • 6
  • 24
  • i was creating this method to check if this method can implement the run method. it is for my project, which has more then one error. i do not want to post so much question first. anyway, i figured out how to make the thread works. thanks. – Myst Jan 22 '14 at 03:30

3 Answers3

3

Since you are trying to update the UI, you need to do it on the UI Thread. You should use something like runOnUiThread() or AsyncTask.

 runOnUiThread(new Runnable()
{
    @Override
    public void run()
    {
            Toast.makeText(MainActivity.this, "display something",
                Toast.LENGTH_LONG).show();
    }
 });

or

Example of AsyncTask

AsyncTask Docs

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

You can't update ui from a background thread. You can update ui from ui thread only.

You can use runOnUiThread. But to just display a toast why do you require a thread?.

http://developer.android.com/guide/components/processes-and-threads.html

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

You cannot do UI changes (like a toast) on a thread that is not the UI thread. Use this instead:

public void run() {
        MainActivity.this.runOnUiThread(new Runnable(){

              @Override
              public void run(){
                    Toast.makeText(MainActivity.this, "display something",
                Toast.LENGTH_LONG).show();
              }
        });

 }

However, why are you creating a whole separate thread to show a toast? You're better off just putting Toast.makeText(MainActivity.this, "display something", Toast.LENGTH_LONG).show(); in your onCreate method.

AxiomaticNexus
  • 6,190
  • 3
  • 41
  • 61