【问题标题】:Running Volley request in background service在后台服务中运行 Volley 请求
【发布时间】:2016-09-19 04:09:15
【问题描述】:

我想使用 Volley 从服务类中提取一些 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) 在 java.lang.reflect.Method.invoke(本机方法) 在 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) 在 java.lang.Class.newInstance(本机方法) 在 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)

【问题讨论】:

  • 你可以尝试用 getApplicationContext() 替换它吗?还请向我们展示来自 logcat 的错误消息,或者如果您将其包装在 try catch 块中,则需要查看错误。
  • 编辑添加 logcat 以了解当我执行 getApplicationContext 时会发生什么
  • 请在这个类中添加其余代码,以便我们了解发生了什么。这是在片段中吗?试试 getActivity().getApplicationContext()
  • @Manny264,不,这是一项服务。我可以在位中添加代码。
  • 很糟糕。服务是否已在 android manifest 中定义?

标签: android android-volley


【解决方案1】:

在清单中定义服务。

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


看看this 的帖子。有了你提供给我们的代码,我能想到的就这些了。

【讨论】:

    【解决方案2】:

    我没有传递正确的上下文。

    【讨论】:

    • 你应该写下你传递的上下文以供将来参考
    猜你喜欢
    • 2015-07-10
    • 2017-03-05
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多