我在我的函数中获取数据。但是,当尝试将数据设置为我的textView时,它会失败。它给出了一个空指针异常。但我在页面顶部初始化了我的textView。任何人都知道为什么会出现这个错误?
Process: com.example.dylangomes.parkingxl, PID: 10078
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.dylangomes.parkingxl.DetailFragment$1.onCallBackRecieved(DetailFragment.java:79)
at com.example.dylangomes.parkingxl.Services.ParkingService$3.onResponse(ParkingService.java:113)
at com.example.dylangomes.parkingxl.Services.ParkingService$3.onResponse(ParkingService.java:108)
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:769)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
这是我的功能
public class DetailFragment extends Fragment {
private String titleText, addressText;
private int freePlacesText;
private TextView title, freeplaces, address;
private ParkingService parkserv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragmentView = inflater.inflate(R.layout.fragment_detail, container, false);
title = fragmentView.findViewById(R.id.title_view);
freeplaces = fragmentView.findViewById(R.id.freePlaces);
address = fragmentView.findViewById(R.id.address);
return fragmentView;
}
public void setId(int id) {
Log.d("Set id function called", "FUnction called");
parkserv = new ParkingService(getActivity());
parkserv.getParkingLotsById(id);
parkserv.setApiCallBackObject(new APICallBackListenerObject() {
@Override
public void onCallBackRecieved(JSONObject jsonObject) {
Log.d("Getting into OnCallback", "OncallBack");
final Gson gson = new Gson();
final Type type = new TypeToken<ParkingLots>() {
}.getType();
final ParkingLots parkinglots = gson.fromJson(jsonObject.toString(), type);
Log.d("ParkingLots", parkinglots.getAddress());
Log.d("ParkingLots", parkinglots + "");
titleText = parkinglots.getTitle();
freePlacesText = parkinglots.getPlacesNow();
addressText = parkinglots.getAddress();
title.setText(titleText);
freeplaces.setText(freePlacesText);
address.setText(addressText);
}
});
}
}
日志会记录我期望的数据但是在我进入视图并崩溃程序之前它会出错