调用方法JobRequest.Builder.schedule()时获取java.lang.IllegalStateException

时间:2018-04-10 05:41:56

标签: java android singleton job-scheduling illegalstateexception

我想使用安卓工作来安排工作。我已经检查过这个样本的样本。

当我尝试在我的应用程序中添加一个Android作业并调用该方法时,会因非法状态异常而崩溃。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.siddhi.creditOneField/com.example.siddhi.creditOneField.Activities.MainActivity}: java.lang.IllegalStateException: You need to call create() at least once to create the singleton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.IllegalStateException: You need to call create() at least once to create the singleton
at com.evernote.android.job.JobManager.instance(JobManager.java:166)
at com.evernote.android.job.JobRequest$Builder.<init>(JobRequest.java:485)
at com.example.siddhi.creditOneField.Activities.TrackingJob.schedulePeriodic(TrackingJob.java:31)
at com.example.siddhi.creditOneField.Activities.MainActivity.setUpUI(MainActivity.java:164)
at com.example.siddhi.creditOneField.Activities.MainActivity.onCreate(MainActivity.java:105)
at android.app.Activity.performCreate(Activity.java:6672)

这是我得到的例外。

主要活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        setUpUI();
}
public void setUpUI()
{
    TrackingJob.schedulePeriodic();
}

跟踪工作:

class TrackingJob extends Job {

    static final String TAG = "tracking";

    @NonNull
    @Override
    protected Result onRunJob(Params params) {

        Intent pi = new Intent(getContext(), GetLocationService.class);

        getContext().startService(pi);

        return Result.SUCCESS;
    }

    static void schedulePeriodic() {
        new JobRequest.Builder(TrackingJob.TAG)
                .setPeriodic(TimeUnit.MINUTES.toMillis(15), TimeUnit.MINUTES.toMillis(15))
                .setUpdateCurrent(true)
                .setPersisted(true)
                .build()
                .schedule();
    }
}

在示例应用程序中,它以相同的方式调用并正常工作,为什么它会在这里崩溃?

1 个答案:

答案 0 :(得分:1)

您必须使用以下代码

创建TrackingJob的对象
  class DemoJobCreator implements JobCreator {

        @Override
        public Job create(String tag) {
            switch (tag) {
                case TrackingJob.TAG:
                    return new TrackingJob ();
                default:
                    return null;
            }
        }
    }

申请类

public class MainApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        JobManager.create(this).addJobCreator(new DemoJobCreator());
    }
}

它将创建作业类的对象,然后您可以添加计划表。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        setUpUI();
}
public void setUpUI()
{
    TrackingJob.schedulePeriodic();
}