6

I have an onClick event in my android app that triggers the following code but it keeps crashing my app. I put it in a thread only because i read that that's supposed to prevent crashing. Also ctx refers to the Activity's context (it's a variable I created in the activity set equal to this. I've read and tried several things. Any help would be awesome. Thanks!

Thread toastThread = new Thread() {
  public void run() {
    Toast alertFailure = Toast.makeText(ctx, "Login Failed", Toast.LENGTH_LONG);
    alertFailure.show();
  }
};
toastThread.start();
Hyrum Hammon
  • 274
  • 1
  • 2
  • 10

6 Answers6

12

You need to use runOnUiThread

Something like

runOnUiThread(new Runnable() {
    public void run()
    {
        Toast.makeText(ctx, toast, Toast.LENGTH_SHORT).show();
    }
});

Toast is a UI element so it needs to run on the UI Thread, not a background Thread.

However, if this is all you are using it for then you don't need a separate Thread just to show a Toast. If you can explain the context of how you are using it then maybe we can help with a better way. Also, if you are inside of your Activity then you don't need a variable for Context. You can use ActivityName.this instead to access the Activity Context

OverShifted
  • 457
  • 1
  • 7
  • 17
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • getting error cannot resolve symbol activity. Am I supposed to put something else there? – Hyrum Hammon Jul 01 '13 at 18:09
  • Sorry, I copied from something else I had and didn't change it quick enough. See my edits. – codeMagic Jul 01 '13 at 18:09
  • check this library `compile 'com.shamanland:xdroid-toaster:0.0.5'`, it doesn't require `runOnUiThread()` or `Context` variable, all routine is gone! just invoke `Toaster.toast(R.string.my_msg);` here is the example: https://github.com/shamanland/xdroid-toaster-example – Oleksii K. Jul 17 '14 at 10:07
0

You likely don't have your ctx variable instantiated, so you are getting NULL Pointer.

You should not put this in a Thread, and is actually a pretty bad idea (knowing you are just getting started).

execute: adb logcat to see your log output.

Booger
  • 18,579
  • 7
  • 55
  • 72
  • How do I see my log in android studio? I just switched from eclipse – Hyrum Hammon Jul 01 '13 at 18:10
  • If you are a beginner, the docs even suggest not relying too much on Android Studio right now since it is a pre-release version. I would suggest sticking with Eclipse for your main development until you are more comfortable with Android development. – codeMagic Jul 01 '13 at 18:12
  • I don't know about Android Studio (and agree you should use Eclipse as a beginner). You can issue that log command from the terminal prompt (outside of the IDE) – Booger Jul 01 '13 at 18:12
0

You don't need a different thread, your ctx variable is probably the one causing it, try using getApplicationContext(), this should work:

import android.widget.Toast;

Toast.makeText(getApplicationContext(), "Login Failed", Toast.LENGTH_LONG).show();
ah008a
  • 1,115
  • 6
  • 11
  • 1
    you cannot show any UI related element in a non-ui thread which is probably why he is getting the crash – tyczj Jul 01 '13 at 18:09
  • This is true, if you are trying to do something else than a Toast for example network related you should use **AsyncTask** otherwise your app will crash, although I'm pretty sure that if you are targetting ICS or JB it wont let you build the project anyway. – ah008a Jul 01 '13 at 18:16
0

Only the mainthread can change the UI. That's why your application crashes. Do your work on the mainthread, and if you are doing something heavy like network or IO, you should use AsyncTask because each thread has 5 seconds to respond.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
kamran hatami
  • 122
  • 1
  • 8
0

Maybe this one can help you.

runOnUiThread(()->Toast.makeText(this,"I am a Toast message",Toast.LENGTH_SHORT).show());
-1

You can do this like this.

((Button)findViewById(R.id.myButton)).setOnClickListener(new OnClickListener(){
    public void onClick(View v) {
        Toast.makeText(MyActivity.this, "Login Failed", Toast.LENGTH_LONG).show();
    }
});

Where:

  • myButton is the id of your view
  • myActivity is your activity

I hope this will help you.

SpdyMx
  • 34
  • 5