由java.lang.RuntimeException引起:无法在未调用Looper.prepare()

时间:2018-04-10 10:43:45

标签: android android-handler jobintentservice

当离线调用此问题时,在JobIntentService内初始化一个处理程序

Caused by java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
       at android.os.Handler.<init>(Handler.java:200)
       at android.os.Handler.<init>(Handler.java:114)
       at JobIntentSericeClass.calculateExtras(DataManager.java:288)
       at JobIntentSericeClass.onHandleBlockingIntent(DataManager.java:499)
       at JobIntentSericeClass.onHandleWork(DataManager.java:100)
       at android.support.v4.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:391)
       at android.support.v4.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:382)
       at android.os.AsyncTask$2.call(AsyncTask.java:295)
       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
       at java.lang.Thread.run(Thread.java:818)

请参阅以下代码:

public class JobIntentServiceClass extends JobIntentService {

@Override
    protected void onHandleWork(@NonNull Intent intent) {
       onHandleBlockingIntent(intent);
    }

private void onHandleBlockingIntent(Intent intent) {
    calculateExtras();
}

private void calculateExtras() {
final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // some async task here
                }
            }
        }, 5000);
}
}

我在初始化处理程序的行上遇到错误。当应用程序离线时也会发生这种情况。在线方案工作正常。看起来很奇怪

0 个答案:

没有答案