我的应用使用了android服务,其中 每当收到firebase通知时,我都会创建一个改造实例并进行网络服务调用。
我的应用程序在前台和后台均可正常运行 收到通知,并且还会触发Web服务。
但是当我的应用处于终止状态时。
我检查android服务是否正在运行,如果没有运行,我会启动该服务并发送广播接收器以触发webservice调用,但是我收到以下错误消息。
由于:java.lang.NullPointerException:尝试调用虚拟 方法'java.lang.Object retrofit2.Retrofit.create(java.lang.Class)' 在空对象引用上
这是因为无法在应用终止条件下创建改造实例。
我该如何解决?
预先感谢
公共类FCM_Background扩展了服务{ 私有静态字符串urlString; 私有静态字符串orderBaseUrl,orderUrl,orderMethod,receiveBaseUrl,receiveUrl,receivedMethod; 私有静态上下文上下文;
private BroadcastReceiver receiver;
private String orderId;
private ArrayList<Integer> orderid;
DecimalFormat form = new DecimalFormat("#.##");
private Toast toastMessage;
private String paymentType;
private String orderComment;
private String LogTag = FCM_Background.class.getSimpleName();
private SQLiteHelper helper;
public static SQLiteDatabase db;
private String location_id;
private String state;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
orderid = new ArrayList<Integer>();
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
location_id = MyPreferenceManager.getInstance(getApplicationContext()).getData(Common.LOCATION_ID);
state = intent.getExtras().getString("FROM");
fetchJsonDataFromRetrofit();
}
};
registerReceiver(receiver, new IntentFilter(Common.FCM_BROADCAST));
return super.onStartCommand(intent, flags, startId);
}
public synchronized void fetchJsonDataFromRetrofit() {
helper = new SQLiteHelper(getApplicationContext());
db = helper.getWritableDatabase();
final ApiInterface listApi = new ApiClient().getClient().create(ApiInterface.class);
Common.validationToken = MyPreferenceManager.getInstance(getApplicationContext()).getData(Common.AUTH_TOKEN);
if (null == orderBaseUrl || null == orderUrl) {
orderBaseUrl = MyPreferenceManager.getInstance(this).getData(Common.DOMAIN_NAME);
orderUrl = "admin/api/orders";
}
最终通话OrderCall = listApi.getOrderDetails(orderBaseUrl,orderUrl,Common.validationToken);
OrderCall.enqueue(new Callback<OrderView>() {
@Override
public void onResponse(Call<OrderView> call, Response<OrderView> response) {
orderBaseUrl = null;
orderUrl = null;
if (response.isSuccessful()) {
ArrayList<Order> orders = response.body().getOrderList();
int orderSize = orders != null ? orders.size() : 0;
String orderType = null;
String orderCreatedDate = null;
if (orderSize > 0) {
orderDetails(orders, orderType, orderCreatedDate);
}
} else {
ActionOnTokenExpiry(response);
}
}
@Override
public void onFailure(Call<OrderView> call, Throwable t) {
}
});
}