数据检索没有任何问题。但是我希望将该数据设置为cardviews。当我尝试执行以下代码时,CustomAdapter类中的“ 试图在空对象引用上调用虚拟方法'int java.util.ArrayList.size()'”是我面临的错误。我的代码是
MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
CustomAdapter customAdapter;
ArrayList<ProductsData> arrayList;
ProgressDialog progressDialog;
String url = "url";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
setUpRecycler();
getData();
}
private void getData() {
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("result here","result here"+response);
try {
progressDialog.dismiss();
JSONObject jsonObject = new JSONObject(response);
arrayList = new ArrayList<>();
JSONArray jsonArray = jsonObject.getJSONArray("products");
for (int i = 0; i <= jsonArray.length();i++)
{
ProductsData productsData = new ProductsData();
JSONObject obj = jsonArray.getJSONObject(i);
productsData.setProductImage(obj.getInt("image"));
productsData.setProductTitle(obj.getString("name"));
productsData.setProductPrice(obj.getString("price"));
arrayList.add(productsData);
}
setUpRecycler();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, ""+error.toString(), Toast.LENGTH_SHORT).show();
}
}
)
{
@Override
public Map<String,String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("Authorization", "1234567890");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void setUpRecycler() {
recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),2));
customAdapter = new CustomAdapter(this,arrayList);
recyclerView.setAdapter(customAdapter);
}
}
CustomAdapter.java
class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private Context context;
private ArrayList<ProductsData> arrayList;
public CustomAdapter(Context context, ArrayList<ProductsData> arrayList) {
this.context= context;
this.arrayList = arrayList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(context).inflate(R.layout.cardviewforproducts,viewGroup,false);
MyViewHolder holder = new MyViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.productImage.setImageResource(arrayList.get(i).getProductImage());
myViewHolder.productName.setText(arrayList.get(i).getProductTitle());
myViewHolder.productPrice.setText(arrayList.get(i).getProductPrice());
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder
{
ImageView productImage;
TextView productName;
TextView productPrice;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
productImage = itemView.findViewById(R.id.productImage);
productName = itemView.findViewById(R.id.productName);
productPrice = itemView.findViewById(R.id.productPrice);
}
}
}
ProductsData.java
public class ProductsData {
private int productImage;
private String productTitle,productPrice;
public int getProductImage() {
return productImage;
}
public void setProductImage(int productImage) {
this.productImage = productImage;
}
public String getProductTitle() {
return productTitle;
}
public void setProductTitle(String productTitle) {
this.productTitle = productTitle;
}
public String getProductPrice() {
return productPrice;
}
public void setProductPrice(String productPrice) {
this.productPrice = productPrice;
}
}
activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</android.support.constraint.ConstraintLayout>
cardviewforproducts.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:elevation="3dp"
app:cardElevation="4dp"
app:cardCornerRadius="1dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<ImageView
android:id="@+id/productImage"
android:layout_width="match_parent"
android:layout_height="150dp"
/>
<TextView
android:id="@+id/productName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB
SSD/Mac OS Sierra)"
android:textColor="#000000" />
<TextView
android:id="@+id/productPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
我希望检索到的产品应设置为卡片浏览量。
但是面临的错误是试图在空对象引用上调用虚拟方法'int java.util.ArrayList.size()'
答案 0 :(得分:0)
您需要全局初始化数组列表
ArrayList<ProductsData> arrayList = new ArrayList<>();
答案 1 :(得分:0)
从setUpRecycler();
删除OnCreate()
因此您的onCreate()如下所示
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
getData();
}
出现问题的原因是,您在getData()
中初始化了arraylist,并在setUpRecycler()
之前调用了getData()
,因此当时arraylist没有初始化。
答案 2 :(得分:0)
您需要在 customAdapter 的 getData()方法中包含notifyDataSetChanged
。因为您要在 setUpRecycler()中设置空适配器,所以您需要调用它。
如下修改您的 getData()方法。您无需在该方法内再次调用 setUpRecycler()。
也将此arrayList = new ArrayList<>();
移动到 setUpRecycler()中,因为只需要调用一次即可。
private void getData() {
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("result here","result here"+response);
try {
progressDialog.dismiss();
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("products");
for (int i = 0; i <= jsonArray.length();i++)
{
ProductsData productsData = new ProductsData();
JSONObject obj = jsonArray.getJSONObject(i);
productsData.setProductImage(obj.getInt("image"));
productsData.setProductTitle(obj.getString("name"));
productsData.setProductPrice(obj.getString("price"));
arrayList.add(productsData);
}
customAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, ""+error.toString(), Toast.LENGTH_SHORT).show();
}
}
)
{
@Override
public Map<String,String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("Authorization", "1234567890");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
还以此修改您的 setUpRecycler()方法。
private void setUpRecycler() {
arrayList = new ArrayList<>();
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),2));
recyclerView.setHasFixedSize(true);
customAdapter = new CustomAdapter(this,arrayList);
recyclerView.setAdapter(customAdapter);
}