gpt4 book ai didi

java - 调用方法 JobRequest.Builder.schedule() 时出现 java.lang.IllegalStateException

转载 作者:行者123 更新时间:2023-12-02 11:23:43 26 4
gpt4 key购买 nike

我想使用 Android 作业来安排作业。我已经检查过这个示例,效果很好。

当我尝试在我的应用程序中添加 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)

这是我遇到的异常。

主要 Activity

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

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

最佳答案

您必须使用以下代码创建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();
}

关于java - 调用方法 JobRequest.Builder.schedule() 时出现 java.lang.IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49746110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com