在后台服务

时间:2016-05-22 06:38:23

标签: android android-volley

我想使用Volley从Service类中提取一些JSON数据,但我一直在服务类的RequestQueue mRequestQueue = Volley.newRequestQueue(this);处收到文件缓存崩溃。

它在我的主要应用程序中运行良好。

修改

  

致命的例外:主要                                                                             处理:com.xxx.yyy,PID:28290                                                                             java.lang.RuntimeException:无法实例化服务   com.idlctech.etherpooler.PoolerService:   java.lang.NullPointerException:尝试调用虚方法   “android.content.Context   空对象上的android.content.Context.getApplicationContext()'   参考                                                                                 在   android.app.ActivityThread.handleCreateService(ActivityThread.java:3121)                                                                                 在android.app.ActivityThread.-wrap5(ActivityThread.java)                                                                                 在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1530)                                                                                 在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                 在android.os.Looper.loop(Looper.java:154)                                                                                 在android.app.ActivityThread.main(ActivityThread.java:5969)                                                                                 at java.lang.reflect.Method.invoke(Native Method)                                                                                 在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:801)                                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)                                                                              引起:java.lang.NullPointerException:尝试调用虚拟   方法'android.content.Context   空对象上的android.content.Context.getApplicationContext()'   参考                                                                                 在   android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)                                                                                 在   xxx.yyy.zzz.PoolerService。(PoolerService.java:38)                                                                                 at java.lang.Class.newInstance(Native Method)                                                                                 在   android.app.ActivityThread.handleCreateService(ActivityThread.java:3118)                                                                                 在android.app.ActivityThread.-wrap5(ActivityThread.java)                                                                                 在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1530)                                                                                 在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                 在android.os.Looper.loop(Looper.java:154)                                                                                 在android.app.ActivityThread.main(ActivityThread.java:5969)

2 个答案:

答案 0 :(得分:1)

在清单中定义服务。

<application ... >
  <service android:name=".ExampleService" />
  ...
</application>

this帖子。使用您提供给我们的代码,我能想到的就是。

答案 1 :(得分:-1)

我没有通过正确的背景。