此类实现SlideShowListener:
RestaurantFragment.java
public class RestaurentFragment extends MasterFragment implements SlideShowListener {
private SlideShowHttpService slideShowHttpService;
// Remaining code here.
@Override
public void onResume() {
super.onResume();
if(Util.isNetworkAvailable(mContext)){
slideShowHttpService = new SlideShowHttpService(mContext);
slideShowHttpService.mSlideShowListener = this;
}else{
CustomSnackBar.display(mContext, "No Connection", "Retry");
}
@Override
public void setSlideShowData(List<ImageSliderPOJO> urlsList, int statusCode) {
Log.d(TAG," image url "+urlsList.get(0).getImageUrl());
}
// Remaining code here.
}
此类抛出空指针异常
SlideShowHttpService.java
public class SlideShowHttpService {
private static final String TAG = SlideShowHttpService.class.getSimpleName();
private Context mContext;
public SlideShowListener mSlideShowListener;
public SlideShowHttpService(Context context){
this.mContext = context;
}
public void parseImageURLLists() {
if (Util.isNetworkAvailable(mContext)) {
final ArrayList<ImageSliderPOJO> urlsList = new ArrayList<>();
FoodRestClient.getURLList("bins/3655b", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String response = new String(responseBody);
try {
JSONArray array = new JSONArray(response);
int arraySize = array.length();
if (arraySize > 0) {
for (int i = 0; i < arraySize; i++) {
JSONObject object = array.getJSONObject(i);
String imageUrl = object.getString("SliderImage");
String targetVendorId = object.getString("TargetVendorId");
urlsList.add(new ImageSliderPOJO(imageUrl, targetVendorId));
}
mSlideShowListener.setSlideShowData(urlsList, statusCode); // Null pointer Exception here
}
} catch (JSONException e) {
Log.v(TAG, "json fetch failure " + e.getMessage());
mSlideShowListener.setSlideShowData(null, statusCode);
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.v(TAG, "slideshow http service failure: " + error.getMessage() + " " + statusCode);
mSlideShowListener.setSlideShowData(null, statusCode);
}
});
}
}
}
这是我的界面SlideShowListener.java
public interface SlideShowListener {
void setSlideShowData(List<ImageSliderPOJO> urlsList, int statusCode);
}
Log Cat错误:
FATAL EXCEPTION: main
Process: com.app.foodmandu, PID: 12051
java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke interface method 'void com.app.foodmandu.model.listener.SlideShowListener.setSlideShowData(java.util.List, int)' on a null object reference
at com.loopj.android.http.AsyncHttpResponseHandler.onUserException(AsyncHttpResponseHandler.java:304)
at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:395)
at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:510)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
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:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void com.app.foodmandu.model.listener.SlideShowListener.setSlideShowData(java.util.List, int)' on a null object reference
at com.app.foodmandu.view.http.SlideShowHttpService$1.onSuccess(SlideShowHttpService.java:56)
at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:351)
at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:510)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
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:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
我在这里做错了什么?为什么mSlideShowListener.setSlideShowData()
方法抛出空指针异常?我已初始化mSlideShowListener
类
onResume()
内RestaurantFragment
答案 0 :(得分:2)
在 RestaurantFragment.java 中 检查列表urlsList不为空,
@Override
public void setSlideShowData(List<ImageSliderPOJO> urlsList, int statusCode) {
if(urlsList!=null)
Log.d(TAG," image url "+urlsList.get(0).getImageUrl());
}
因为在服务中
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.v(TAG, "slideshow http service failure: " + error.getMessage() + " " + statusCode);
mSlideShowListener.setSlideShowData(null, statusCode);//you're passing null to list
}
甚至在捕捉方法中,你做同样的事情mSlideShowListener.setSlideShowData(null, statusCode);
答案 1 :(得分:0)
实际上问题是,我初始化了一个对象&#34; slideShowHttpService&#34;但是忘了调用方法&#34; parseImageURLLists()&#34;在它上面。愚蠢的我