我使用intentService在每个特定时间段定期更新新闻... 当我尝试通过getResources从字符串资源获取URL时,应用程序崩溃并返回null
这是IntentService
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.jerry.studentunion.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import announcements.Announcement;
import announcements.AnnouncementsBag;
public class AnnouncementIntentService extends IntentService {
private final Resources URL = getResources(); //line 24
private JsonObjectRequest jsObjRequest= null;
private AnnouncementsBag announcementData = new AnnouncementsBag();
public AnnouncementIntentService() {
super("AnnouncementIntentService");
jsObjRequest = new JsonObjectRequest(Request.Method.GET,
"Place Holder for URL ",
null,
new Response.Listener<JSONObject>() {
// The Listeners goes here
});
jsObjRequest.setShouldCache(false);
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("BroadCast", "SecondStep");
Intent broadCastAction = new Intent();
broadCastAction.setAction(GlobalBroadcast.ANNOUNCEMENT_START_UPDATING_ACTION);
sendBroadcast(broadCastAction);
CustomRequestQueue.getInstance(getApplicationContext())
.addToRequestQueue(jsObjRequest);
}
}
完整的logcat崩溃错误
09-06 00:44:25.350 15831-15831/com.example.jerry.studentunion E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate service util.AnnouncementIntentService: java.lang.NullPointerException
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2650)
at android.app.ActivityThread.access$1600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at util.AnnouncementIntentService.<init>(AnnouncementIntentService.java:24)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2647)
at android.app.ActivityThread.access$1600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
我使用广播接收器在BOOT_COMPLETED操作时使用警报管理器启动此服务。
答案 0 :(得分:2)
在您发布的代码中,您实际上并未使用调用URL
的字段getResources()
,因此您可以轻松删除它。
但是,如果您需要获取IntentService
中的资源,只需在实际需要时调用getResources()
,然后将其存储在本地变量中,如下所示:
....
@Override
protected void onHandleIntent(Intent intent) {
Resources resources = getResources();
}
...
您获得NullPointerException
的原因是因为您在getResources()
创建之前拨打了IntentService
。如果您坚持将其存储在字段中,则可以通过覆盖onCreate()
并将来电置于getResources()
来完成此操作。