-1

I am trying to implement toast in connection handler class

public class server implements Runnable {
private static final String TAG = "myLogs";
.....
public void run()
{
.....
while (true) {
    try {

       client = server.accept();
        // here i want to show message, when client is connected
         Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
          Log.d(TAG, "client connected....");

but i got error on makeText and getApplicationContext(); they are underlined..

chajka
  • 51
  • 2
  • 4
  • 13

6 Answers6

1

You cannot get getApplicationContext() in a runnable, only in an activity or service. To get application context in a runnable you should pass the context of your activity to your runnable.

But in your case you want to show a toast in the activity context. In order to do this pass the activity object and call runOnUIThread method. Give your activity as context to the toast:

public class Server implements Runnable {
private static final String TAG = "myLogs";
private Activity myActivity;

  public Server(final Activity activity) {
    this.myActivity = activity;
  }

.....
public void run()
{
.....
while (true) {
    try {

       client = server.accept();
        // here i want to show message, when client is connected
        myActivity.runOnUiThread(new Runnable() {
          public void run() {
            Toast.makeText(myActivity, "msg msg", Toast.LENGTH_SHORT).show();
            Log.d(TAG, "client connected....");    
          }
       });

This is also described here: Android: Toast in a thread

Community
  • 1
  • 1
L. G.
  • 9,642
  • 7
  • 56
  • 78
  • i tried this variant, i got no errors, but i didn't get any toast msg on my phone and my app crashed and i have the same problem implementing other functions, like thread.stop(), finish() app , where can be problem? i have no idea – chajka Apr 26 '13 at 15:06
  • I didn't fully answer your question sorry, only the part to get application context. I corrected it to show a toast from a thread or runnable on an activity. – L. G. May 02 '13 at 15:06
0

See the explanation here

Maybe you should pass an Activity context using the constructor or something, but you can't use applicationcontext for toast.

Community
  • 1
  • 1
Analizer
  • 1,594
  • 16
  • 30
0

I think your getApplicationContext() method is probably returning NULL. I think this is because you are running the toast from a thread, which would be off the UI thread.

You will need to call this method from somewhere in your application that has UI context (meaning somewhere where the system can get a handle to the UI thread - or the place where it needs to display the Toast).

Try to get the application context before creating the Toast (perhaps using this syntax):

Context ctx =getApplicationContext();

Then, ensure it is not NULL before trying to display your toast.

If it is in fact NULL, you will need to pass the context (use that method somewhere you do have context, and pass it into your thread as a parameter)

Booger
  • 18,579
  • 7
  • 55
  • 72
0

use classname.this it surely works

Supreet
  • 2,451
  • 8
  • 25
  • 42
  • now i got makeText underlined.. Toast.makeText(server.this, "msg msg", Toast.LENGTH_SHORT).show(); – chajka Apr 26 '13 at 13:33
  • it won't work, server class has nothing to do with any activity, it doesn't have an activity context – Analizer Apr 26 '13 at 14:01
0

First check you do the correct import (i think it's import android.widget.Toast; or you can use import android.widget.*; to be sure). Then try to use this instead of getApplicationContext(), or activityname.this.

TheBeps
  • 36
  • 4
0

Try this.

Context context; context =this;

Toast.makeText(context, "msg msg", Toast.LENGTH_SHORT).show();

or

Toast.makeText(ClassName.this, "msg msg", Toast.LENGTH_SHORT).show();

or

Toast.makeText(this.getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();