0

我无法从我的 android Service 类中打印出简单的 Toast 消息:

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

此运行方法位于实现Runnable该类的 Service 内部,因此我正在使用handler.post,但是无法在内部run()方法内部进行任何工作。我认为这handler.post会发布会起作用,有什么问题?

其余代码和 logcat 打印输出如下;

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();
                     }
            }); 

    }
}

日志猫;

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)
4

3 回答 3

1

改变这个

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

Toast.makeText(LotteryServer.this, "thread is running", Toast.LENGTH_LONG).show();
于 2013-02-21T08:35:51.987 回答
0
Toast.makeText(context, "thread is running", Toast.LENGTH_LONG).show();

取而代之的是上下文使用 getApplicationContxt() 希望这可以工作..因为这将获得整个上下文的范围。试试看

于 2013-02-21T06:29:55.553 回答
0

如果不起作用,请使用 runOnUIThread(Runnable),试试这个

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();
于 2013-02-21T06:37:18.673 回答