0

I am new here to Android. My application is crashing with errors after sleep runs (5000) and then my toast is not showing in my finally block. What is causing these errors in my code?

public class LoginActivity extends ActionBarActivity {

TextView textview;
Toast toast;

@Override
protected void onCreate(Bundle ThisisLoginActivity) {
    super.onCreate(ThisisLoginActivity);
    setContentView(R.layout.activity_login);

    textview = (TextView) findViewById(R.id.textView1);

    Thread thread = new Thread() {
        public void run() {
            try{
                sleep(5000);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
            finally {
                toast = Toast.makeText(getApplicationContext(), "Sleep Runing", Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    };
    thread.start();
}
}

Here is my LogCat:

01-08 22:30:32.782: E/Trace(3503): error opening trace file: No such file or directory (2)
01-08 22:30:33.322: I/Choreographer(3503): Skipped 47 frames!  The application may be doing too                     much work on its main thread.
01-08 22:30:33.402: D/gralloc_goldfish(3503): Emulator without GPU emulation detected.
01-08 22:30:33.531: I/Choreographer(3503): Skipped 62 frames!  The application may be doing too      much work on its main thread.
01-08 22:30:39.122: I/Choreographer(3503): Skipped 31 frames!  The application may be doing too much work on its main thread.
01-08 22:30:39.272: I/Choreographer(3503): Skipped 43 frames!  The application may be doing too much work on its main thread.
01-08 22:30:45.772: W/dalvikvm(3503): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
01-08 22:30:45.792: E/AndroidRuntime(3503): FATAL EXCEPTION: Thread-153
01-08 22:30:45.792: E/AndroidRuntime(3503): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
01-08 22:30:45.792: E/AndroidRuntime(3503):     at android.os.Handler.<init>(Handler.java:197)
01-08 22:30:45.792: E/AndroidRuntime(3503):     at android.os.Handler.<init>(Handler.java:111)
01-08 22:30:45.792: E/AndroidRuntime(3503):     at android.widget.Toast$TN.<init>(Toast.java:324)
01-08 22:30:45.792: E/AndroidRuntime(3503):     at android.widget.Toast.<init>(Toast.java:91)
01-08 22:30:45.792: E/AndroidRuntime(3503):     at android.widget.Toast.makeText(Toast.java:238)
01-08 22:30:45.792: E/AndroidRuntime(3503):     at com.example.incrementdecrement.LoginActivity$1.run(LoginActivity.java:48)
01-08 22:30:46.462: I/Choreographer(3503): Skipped 34 frames!  The application may be doing too much work on its main thread.
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97

3 Answers3

1

You cannot directly manipulate the user interface from another thread. The main thread is responsible for all UI changes.

For a solution to a similiar problem see: Android: Toast in a thread

Community
  • 1
  • 1
0

Just use Activity's runOnUiThread method from your thread:

runOnUiThread(new Runnable() {
    public void run()
    {
        Toast.makeText(LoginActivity.this, "Sleep Runing", Toast.LENGTH_SHORT).show();
    }
});
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
0

Method in onCreate :

private void toastPublic(final String message){
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
       public void run() {
          Toast.makeText(getBaseContext(),""+message, 
             4 /*Toast.LENGTH_SHORT*/).show();
    }});
}

Next : use in inside Thread

alimogh
  • 1
  • 2