在我的应用程序中,我一直在使用改造我的webservice调用.它工作正常,但当应用程序进入后台时它崩溃并得到错误日志,因为,
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..??
这是我的Retrofit客户端类,
public class RetrofitClient {
private static RetrofitCommonService RETROFIT_CLIENT;
public RetrofitClient() {}
public static RetrofitCommonService getInstance() {
//if REST_CLIENT is null then set-up again.
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)
//.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
RETROFIT_CLIENT = restAdapter.create(RetrofitCommonService.class);
}
}
这是我的Retrofit Service界面,
public interface RetrofitCommonService {
@FormUrlEncoded
@POST("/user")
void doRegistration(@Field("user[mobile_number]") String phone, @Field("user[new_uid]") String imei,
Callback response);
}
这样我就可以通过实施改造回调的活动和活动来调用Retrofit服务.
RetrofitCommonService mRetrofitCommonService = RetrofitClient.getInstance();
mRetrofitCommonService.doRegistration(mPhoneNumber, getDeviceId(), this);
public class RegistrationActivity extends Activity implements Callback{
}
@Override
public void success(RetrofitResponse retrofitResponse, Response response) {
}
@Override
public void failure(RetrofitError error) {
}
解决方法:
解决了这个问题.
在我的活动中,我有两个Retrofit服务操作,这两个服务在相同的Retrofit Callback方法上进行中继.当一个服务试图同时获得回调时,这些导致空指针被其他服务使用回调.我已经删除了回调方法并使用了Retrofit服务的返回对象.然后不需要中继回调方法.
这对我有用……
标签:android,nullpointerexception,callback,retrofit,api
来源: https://codeday.me/bug/20190623/1271929.html