我一直试图获得数据上的RecycleView一天了,但它不工作。出现错误:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example。java.lang.NullPointerException:尝试在com.example.tokenauth.Home.access$000(Home.java:25) at com.example.tokenauth.Home$1. onresponse (Home.java:56) at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)在空对象引用上调用虚拟方法'void androidx.recyclerview. setadapter (androidx.recyclerview.widget.RecyclerView$Adapter)'android.os.Handler.handleCallback(Handler.java:888) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:213) at android.app.ActivityThread.main(ActivityThread.java:8178) at java.lang.reflect.Method。调用(本机方法)在com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)请解释我的错误P.S对不起我的英语
JSON类型
{
"list":[
{
"Id":2,
"Name":"temp",
"Description":"Danger"
},
{
"Id":1,
"Name":"Water",
"Description":"Danger"
}
],
"pageInfo":{
"totalRows":2,
"page":1,
"pageSize":25,
"isFirstPage":true,
"isLastPage":true
}
}
Interface.class
@GET("views/Notification")
Call<NotificationArray>getCarsJson();
ModelArray.class
public class NotificationArray {
private CarsModel[] list;
public CarsModel[] getList() {
return list;
}
public void setMoviz(CarsModel[] list) {
this.list = list;
}
}
Home.class
public class Home extends AppCompatActivity {
RecyclerView recyclerView;
List<CarsModel> carsModels;
private CarsAdapter carsAdapter;
private RecyclerView cars_recyclerview;
NotificationArray notificationArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
cars_recyclerview=(RecyclerView)findViewById(R.id.cars_recyclerview);
carsModels = new ArrayList<>();
getCarsResponse();
}
public void getCarsResponse(){
APIService service = ApiClient.getClient().create(APIService.class);
Call<NotificationArray> call=service.getCarsJson();
call.enqueue(new Callback<NotificationArray>() {
@Override
public void onResponse(Call<NotificationArray> call, Response<NotificationArray> response) {
NotificationArray notificationArray= response.body();
carsModels= new ArrayList<>(Arrays.asList(notificationArray.getList()));
PutDataIntoRecyclerView(carsModels);
Toast.makeText(Home.this,"Success",Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<NotificationArray> call, Throwable t) {
Toast.makeText(Home.this,"Failed",Toast.LENGTH_SHORT).show();
Log.e("failure",t.getLocalizedMessage());
}
});
}
private void PutDataIntoRecyclerView(List<CarsModel> carsModels) {
CarsAdapter carsAdapter = new CarsAdapter( this, carsModels);
cars_recyclerview.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(carsAdapter);
}
Model.class
public class CarsModel {
@SerializedName("Id")
@Expose
private String Id;
@SerializedName("Name")
@Expose
private String Name;
@SerializedName("Description")
@Expose
private String Description;
public String getId() {
return Id;
}
public void setId(String Id) {
this.Id = Id;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getDescription() {
return Description;
}
public void getDescription(String Description) {
this.Description = Description;
}
}
Adapter.class
public class CarsAdapter extends RecyclerView.Adapter<CarsAdapter.ViewHolder> {
private List<CarsModel> carsModels;
private Context context;
public CarsAdapter(Context context, List<CarsModel> carsModels) {
this.carsModels=carsModels;
this.context=context;
}
@NonNull
@Override
public CarsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cars_list_item,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CarsAdapter.ViewHolder viewHolder, int i) {
viewHolder.car_name.setText(carsModels.get(i).getName());
viewHolder.car_desc.setText(carsModels.get(i).getDescription());
}
@Override
public int getItemCount() {
return carsModels.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView car_name,car_desc;
public ViewHolder(@NonNull View itemView) {
super(itemView);
car_name=(TextView) itemView.findViewById(R.id.car_name);
car_desc=(TextView)itemView.findViewById(R.id.car_desc);
}
}
}
我一直试图获得数据上的RecycleView一天了,但它不工作。出现错误:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example。java.lang.NullPointerException:尝试在com.example.tokenauth.Home.access$000(Home.java:25) at com.example.tokenauth.Home$1. onresponse (Home.java:56) at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)在空对象引用上调用虚拟方法'void androidx.recyclerview. setadapter (androidx.recyclerview.widget.RecyclerView$Adapter)'android.os.Handler.handleCallback(Handler.java:888) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:213) at android.app.ActivityThread.main(ActivityThread.java:8178) at java.lang.reflect.Method。调用(本机方法)在com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)请解释我的错误P.S对不起我的英语
将PutDataIntoRecyclerView
(函数的最后一行)中的回收器视图对象从recyclerView
->cars_recyclerview
,您已经创建了适配器的对象两次,第一次在全局作用域,第二次在PutDataIntoRecyclerView
方法。你还需要删除在全局范围内创建的recyclerview对象(RecyclerView recyclerView;
),如果你不使用它。
Inside Your Home class:
private void PutDataIntoRecyclerView(List<CarsModel> carsModels) {
CarsAdapter carsAdapter = new CarsAdapter( this, carsModels);
cars_recyclerview.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(carsAdapter);
}
在最后一行代码中将recyclerView.setAdapter(carsAdapter);
更改为cars_recyclerview.setAdapter(carsAdapter);
使它:
private void PutDataIntoRecyclerView(List<CarsModel> carsModels) {
CarsAdapter carsAdapter = new CarsAdapter( this, carsModels);
cars_recyclerview.setLayoutManager(new LinearLayoutManager(this));
cars_recyclerview.setAdapter(carsAdapter);
}