In my application, I used the modification for my calls on the Internet. It works fine, but when the application goes into the background, it crashes and receives an error log as
java.lang.NullPointerException: Attempt to invoke interface method 'void retrofit.Callback.failure(retrofit.RetrofitError)' on a null object reference
at retrofit.CallbackRunnable$2.run(CallbackRunnable.java:53)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Can anyone help me to overcome this issue..??
This is my Retrofit client class,
public class RetrofitClient {
private static RetrofitCommonService RETROFIT_CLIENT;
public RetrofitClient() {}
public static RetrofitCommonService getInstance() {
if (RETROFIT_CLIENT == null) {
setupRestClient();
}
return RETROFIT_CLIENT;
}
private static void setupRestClient() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setClient(new OkClient(new OkHttpClient()))
.setEndpoint(Constants.BASE_URL)
.build();
RETROFIT_CLIENT = restAdapter.create(RetrofitCommonService.class);
}
}
This is my retrofit service interface,
public interface RetrofitCommonService {
@FormUrlEncoded
@POST("/user")
void doRegistration(@Field("user[mobile_number]") String phone, @Field("user[new_uid]") String imei,
Callback<RetrofitResponse> response);
}
And in this way, I make a call to the Retrofit service from my activity and activity, implementing callbacks.
RetrofitCommonService mRetrofitCommonService = RetrofitClient.getInstance();
mRetrofitCommonService.doRegistration(mPhoneNumber, getDeviceId(), this);
public class RegistrationActivity extends Activity implements Callback<RetrofitResponse>{
}
@Override
public void success(RetrofitResponse retrofitResponse, Response response) {
}
@Override
public void failure(RetrofitError error) {
}
source
share