0

I'm not able to print out a simple Toast message from my android Service class:

 @Override
public void run() {
    handler.post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(context, "thread is running", Toast.LENGTH_LONG).show();
        }
    }); 
  }

This run method is inside of a Service that is implementing the Runnable class, for that reason I am using handler.post, however cannot get anything to work inside of the inner run() method. I thought that handler.post would post would work, what is wrong?

the rest of the code and the logcat printout is below;

public class LotteryServer extends Service implements Runnable {

            Handler handler = new Handler();
            Context context = this;

            @Override
            public int onStartCommand(Intent intent, int flags, int startId){
                 super.onStartCommand(intent, startId, startId);
                 Toast.makeText(context, "My Service Started", Toast.LENGTH_LONG).show();
                 return START_STICKY;
            }

            @Override
            public IBinder onBind(Intent arg0) {
                 // TODO Auto-generated method stub
                 return null;
            }
            @Override   
            public void onDestroy() {
                super.onDestroy();
                Toast.makeText(context, "My Service Stopped", Toast.LENGTH_LONG).show();
            }
            @Override
            public void run() {
            handler.post(new Runnable() {
                     @Override
                     public void run() {
                          Toast.makeText(context, "thread is running",     Toast.LENGTH_LONG).show();
                     }
            }); 

    }
}

LogCat;

02-21 15:09:33.279: E/AndroidRuntime(24038): FATAL EXCEPTION: main
02-21 15:09:33.279: E/AndroidRuntime(24038): java.lang.NullPointerException
02-21 15:09:33.279: E/AndroidRuntime(24038): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
02-21 15:09:33.279: E/AndroidRuntime(24038):    at android.widget.Toast.<init>(Toast.java:92)
02-21 15:09:33.279: E/AndroidRuntime(24038):    at android.widget.Toast.makeText(Toast.java:233)
02-21 15:09:33.279: E/AndroidRuntime(24038):    at com.lottery.clientplayer.LotteryServer$1.run(LotteryServer.java:44)
02-21 15:09:33.279: E/AndroidRuntime(24038):    at android.os.Handler.handleCallback(Handler.java:605)
02-21 15:09:33.279: E/AndroidRuntime(24038):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 15:09:33.279: E/AndroidRuntime(24038):    at android.os.Looper.loop(Looper.java:137)
02-21 15:09:33.279: E/AndroidRuntime(24038):    at android.app.ActivityThread.main(ActivityThread.java:4699)
02-21 15:09:33.279: E/AndroidRuntime(24038):    at java.lang.reflect.Method.invokeNative(Native Method)
02-21 15:09:33.279: E/AndroidRuntime(24038):    at java.lang.reflect.Method.invoke(Method.java:511)
02-21 15:09:33.279: E/AndroidRuntime(24038):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
02-21 15:09:33.279: E/AndroidRuntime(24038):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-21 15:09:33.279: E/AndroidRuntime(24038):    at dalvik.system.NativeStart.main(Native Method)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kevik
  • 9,181
  • 19
  • 92
  • 148

3 Answers3

1

Change this

Toast.makeText(context, "thread is running", Toast.LENGTH_LONG).show();

to

Toast.makeText(LotteryServer.this, "thread is running", Toast.LENGTH_LONG).show();
ThePCWizard
  • 3,338
  • 2
  • 21
  • 32
0
Toast.makeText(context, "thread is running", Toast.LENGTH_LONG).show();

instead context use getApplicationContxt() hope this can work.. because tht will get a scop of whole Context. try it

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
Mit Bhatt
  • 1,605
  • 2
  • 13
  • 24
0

Use runOnUIThread(Runnable) if that doesnot work, try this

Handler mHandler = new Handler();
final Runnable mUpdateResults = new Runnable() {
public void run() {
    Toast(this, message, duration).show();
}

new Thread() {
 public void run() {
    mHandler.post(mUpdateResults);
 }
}.start();
techieWings
  • 2,045
  • 2
  • 16
  • 18
  • runOnUiThread does not work in a service, it is giving compile error, about the code you posed, tried it and it will crash if I put the Toast inside the run method() – Kevik Feb 21 '13 at 06:54
  • Handler mHandler = new Handler(); final Runnable mUpdateResults = new Runnable() { public void run() { // Toast.makeText(context, "My Service Stopped", Toast.LENGTH_LONG).show(); } }; @Override public void run() { mHandler.post(mUpdateResults); } // end run method – Kevik Feb 21 '13 at 06:54