简体   繁体   English

将凌空Jsonobjects添加到CardView并将cardView添加到RecyclerView

[英]add volley Jsonobjects to CardView and add cardView to RecyclerView

I'm trying to add Jsonobjects which i have parsed using volley from network and then add that card view to a recyclerView, but when I do so i got NullPointerException. 我试图添加我已经使用网络凌空分析的Jsonobjects,然后将该卡视图添加到recyclerView中,但是当我这样做时,我得到了NullPointerException。 my search.java: 我的search.java:

public class search extends AppCompatActivity {
    public RecyclerView recyclerView;
    RequestQueue requestQueue;
    itemsAdapter itemsAdapter;
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        String url = "http://dl.naserpour.ir/WEBSERVICE/KETABSARA/all.php";
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        requestQueue = Volley.newRequestQueue(this);
        JsonArrayRequest req = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                final List<items> items = new ArrayList<>();
                for (int i = 0; i <= response.length(); i++) {
                    try {
                        JSONObject obj = response.getJSONObject(i);
                        String NAME = obj.getString("name");
                        String WRITER = obj.getString("writer");
                        String IMAGE = "http://dl.naserpour.ir/WEBSERVICE/KETABSARA/upload/images/images" + obj.getString("img");
                        items.add(new items(NAME, WRITER, IMAGE));

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    itemsAdapter = new itemsAdapter(items);
                }
                recyclerView.setAdapter(itemsAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(req);

    }

    public class items {
        private String name, writer, image;

        public items(String name, String writer, String image) {
            this.name = name;
            this.writer = writer;
            this.image = image;
        }
    }

    private class itemHolder extends RecyclerView.ViewHolder {
        private TextView searchname, searchwriter;
        private ImageView searchimage;
        public itemHolder(View itemView) {
            super(itemView);
            searchimage = (ImageView)findViewById(R.id.searchimage);
            searchname = (TextView)findViewById(R.id.searchname);
            searchwriter = (TextView)findViewById(R.id.searchwriter);

        }

    }

    private class itemsAdapter extends RecyclerView.Adapter<itemHolder> {
        private List<items> items;

        public itemsAdapter(List<items> items) {
            this.items = items;
        }

        @Override
        public itemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(search.this);
            View v = inflater.inflate(R.layout.searchlist, parent, false);
            return new itemHolder(v);
        }

        @Override
        public void onBindViewHolder(final itemHolder holder, int position) {
            items item = items.get(position);
            holder.searchname.setText(item.name);
            holder.searchwriter.setText(item.writer);
            ImageRequest req = new ImageRequest(item.image, new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap response) {
                    holder.searchimage.setImageBitmap(response);
                }
            }, 0, 0, ImageView.ScaleType.CENTER_CROP, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            });
            requestQueue.add(req);
        }

        @Override
        public int getItemCount() {
            return items.size();
        }
    }
}

my layout: 我的布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:layoutDirection="rtl"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="ir.naserpour.ketabsara.search">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/search"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/shape"
        android:hint="جستجو"
        android:drawableLeft="@android:drawable/ic_menu_search"
        android:paddingLeft="15dp"
        android:paddingRight="15dp" />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

my searchlist layout: 我的搜索列表布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layoutDirection="rtl">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="180dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="120dp"
                android:layout_height="match_parent"
                android:id="@+id/searchimage"
                android:scaleType="centerCrop" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center_vertical"
                android:paddingRight="10dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text=""
                    android:id="@+id/searchname"
                    android:layout_weight="1"
                    android:gravity="center_vertical|right" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text=""
                    android:id="@+id/searchwriter"
                    android:layout_weight="1"
                    android:gravity="center_vertical|right" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

my logcat(errors are on lines 112, 129): 我的logcat(错误出现在第112、129行):

Process: ir.naserpour.ketabsara, PID: 15311
java.lang.NullPointerException
    at ir.naserpour.ketabsara.search$itemsAdapter.onBindViewHolder(search.java:129)
    at ir.naserpour.ketabsara.search$itemsAdapter.onBindViewHolder(search.java:112)
    at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
    at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)

can you please help me with this? 你能帮我吗?

The error was on the itemHolder class, you should find the View id's by passing the view object of itemHolder; 错误是在itemHolder类上,您应该通过传递itemHolder的视图对象来找到视图ID;

        private class itemHolder extends RecyclerView.ViewHolder {
        private TextView searchname, searchwriter;
        private ImageView searchimage;
        public itemHolder(View itemView) {
        super(itemView);
        searchimage = (ImageView)itemView.findViewById(R.id.searchimage);
        searchname = (TextView)itemView.findViewById(R.id.searchname);
        searchwriter = (TextView)itemView.findViewById(R.id.searchwriter);
        }
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM