【发布时间】: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 中定义?