Volley 中的 JsonArrayRequest 不会将任何值传递给 ListView

我正在尝试在我的项目中使用Volley,我曾经从PHP中获取JSON数组编码并将其改编为.我遵循了此页面的教程:https://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/JsonArrayRequest()ListView


我的应用程序崩溃了,下面是错误。


2019-03-18 15:34:51.706 14999-14999/com.example.smartparkingapp E/AndroidRuntime: FATAL EXCEPTION: main

    Process: com.example.smartparkingapp, PID: 14999

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.app.Activity.getSystemService(java.lang.String)' on a null object reference

        at com.example.smartparkingapp.adapter.CarListAdapter.getView(CarListAdapter.java:48)

        at android.widget.AbsListView.obtainView(AbsListView.java:3180)

        at android.widget.ListView.makeAndAddView(ListView.java:2197)

        at android.widget.ListView.fillDown(ListView.java:824)

        at android.widget.ListView.fillFromTop(ListView.java:885)

        at android.widget.ListView.layoutChildren(ListView.java:1924)

        at android.widget.AbsListView.onLayout(AbsListView.java:2952)

        at android.view.View.layout(View.java:21054)

        at android.view.ViewGroup.layout(ViewGroup.java:6440)

        at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1915)

        at android.view.View.layout(View.java:21054)

        at android.view.ViewGroup.layout(ViewGroup.java:6440)

        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)

        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)

        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)

        at android.view.View.layout(View.java:21054)

        at android.view.ViewGroup.layout(ViewGroup.java:6440)

        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)

        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)


Cats萌萌
浏览 70回答 1
1回答

杨魅力

您正在尝试在适配器中使用空值。将活动对象从调用活动传递到 CarListAdapter 构造函数中activity类似的东西:在你的家庭碎片carListAdapter = new CarListAdapter(this, carList, getActivity());并在您的 CarListAdapter 中:public class CarListAdapter extends BaseAdapter {&nbsp; &nbsp; private Activity activity;&nbsp; &nbsp; private Fragment fragment;&nbsp; &nbsp; private LayoutInflater layoutInflater;&nbsp; &nbsp; private List<Car> carItems;&nbsp; &nbsp; public CarListAdapter(Fragment fragment, List<Car> carItems, Activity activity) {&nbsp; &nbsp; &nbsp; &nbsp; this.fragment = fragment;&nbsp; &nbsp; &nbsp; &nbsp; this.carItems = carItems;&nbsp; &nbsp; &nbsp; &nbsp; this.activity = activity;&nbsp; &nbsp; }.....这是根据您显示的错误
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java