gpt4 book ai didi

java - 将volley Jsonobjects添加到CardView并将cardView添加到RecyclerView

转载 作者:行者123 更新时间:2023-12-01 09:23:52 25 4
gpt4 key购买 nike

我正在尝试添加我使用网络中的 Volley 解析的 Jsonobjects,然后将该卡片 View 添加到 recyclerView,但是当我这样做时,我得到了 NullPointerException。我的搜索.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();
}
}
}

我的布局:

    <?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>

我的搜索列表布局:

<?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>

我的 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)

你能帮我解决这个问题吗?

最佳答案

错误发生在 itemHolder 类上,您应该通过传递 itemHolder 的 View 对象来查找 View 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);
}
}

关于java - 将volley Jsonobjects添加到CardView并将cardView添加到RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39996555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com