RecyclerView:没有附加适配器;在适配器中创建充气器时跳过布局应用程序关闭
RecyclerView: No adapter attached; skipping layout App closing when it is creating inflater in Adapter
正在尝试创建 RecyclerView 并在 Fragment 中使用 Retrofit2 填充数据
但是当我 运行 应用程序并转到该片段时它会关闭,
当我在适配器的 onCreateViewHolder
的以下行中调试应用程序关闭时
LayoutInflater inflater = LayoutInflater.from( context );
出现以下错误
RecyclerView: No adapter attached; skipping layout
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
以下是片段的代码
ApiConfig apiConfig = AppConfig.getRetrofit().create(ApiConfig.class);
Call<List<TCImages>> call = apiConfig.getImages();
call.enqueue( new Callback<List<TCImages>>() {
@Override
public void onResponse(Call<List<TCImages>> call, Response<List<TCImages>> response) {
tcImages = response.body();
recyclerView = (RecyclerView) getView().findViewById( R.id.recyclerView );
manager = new LinearLayout( getContext() );
tcAdapter = new TCAdapter( tcImages, getActivity() );
recyclerView.setAdapter( tcAdapter );
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
@Override
public void onFailure(Call<List<TCImages>> call, Throwable t) {
}
} );
下面是我的适配器代码
package com.example.tc;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.tc.networking.TCImages;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
public class TCAdapter extends RecyclerView.Adapter<TCAdapter.MyViewHolder> {
List<TCImages> tcImages;
Context context;
public TCAdapter(List<TCImages> tcImages, FragmentActivity activity){
this.tcImages = tcImages;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from( context );
View view = inflater.inflate( R.layout.item_tc_recycler, parent, false );
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.textView.setText( tcImages.get( position ).getEmail() );
}
@Override
public int getItemCount() {
return tcImages.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public MyViewHolder(@NonNull View itemView) {
super( itemView );
textView = (TextView) itemView.findViewById( R.id.txtItem );
}
}
}
我正处于学习阶段,我正在获取 Activity 的代码,我正在将它们转换为 Fragment。
从父级获取上下文。
View view = LayoutInflater.from(parent.context).inflate(R.layout.car_row_layout, parent, false)
或者在适配器构造函数中从片段或activity传递上下文,你只是声明上下文而不是初始化有问题
List<TCImages> tcImages;
Context context;
public TCAdapter(List<TCImages> tcImages, FragmentActivity activity){
this.tcImages = tcImages;
}
您是否为 Context context;
设置过值?
可能您想这样做:
public TCAdapter(List<TCImages> tcImages, Context context){
this.tcImages = tcImages;
this.context = context;
}
正在尝试创建 RecyclerView 并在 Fragment 中使用 Retrofit2 填充数据 但是当我 运行 应用程序并转到该片段时它会关闭,
当我在适配器的 onCreateViewHolder
的以下行中调试应用程序关闭时LayoutInflater inflater = LayoutInflater.from( context );
出现以下错误
RecyclerView: No adapter attached; skipping layout
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
以下是片段的代码
ApiConfig apiConfig = AppConfig.getRetrofit().create(ApiConfig.class);
Call<List<TCImages>> call = apiConfig.getImages();
call.enqueue( new Callback<List<TCImages>>() {
@Override
public void onResponse(Call<List<TCImages>> call, Response<List<TCImages>> response) {
tcImages = response.body();
recyclerView = (RecyclerView) getView().findViewById( R.id.recyclerView );
manager = new LinearLayout( getContext() );
tcAdapter = new TCAdapter( tcImages, getActivity() );
recyclerView.setAdapter( tcAdapter );
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
@Override
public void onFailure(Call<List<TCImages>> call, Throwable t) {
}
} );
下面是我的适配器代码
package com.example.tc;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.tc.networking.TCImages;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
public class TCAdapter extends RecyclerView.Adapter<TCAdapter.MyViewHolder> {
List<TCImages> tcImages;
Context context;
public TCAdapter(List<TCImages> tcImages, FragmentActivity activity){
this.tcImages = tcImages;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from( context );
View view = inflater.inflate( R.layout.item_tc_recycler, parent, false );
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.textView.setText( tcImages.get( position ).getEmail() );
}
@Override
public int getItemCount() {
return tcImages.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public MyViewHolder(@NonNull View itemView) {
super( itemView );
textView = (TextView) itemView.findViewById( R.id.txtItem );
}
}
}
我正处于学习阶段,我正在获取 Activity 的代码,我正在将它们转换为 Fragment。
从父级获取上下文。
View view = LayoutInflater.from(parent.context).inflate(R.layout.car_row_layout, parent, false)
或者在适配器构造函数中从片段或activity传递上下文,你只是声明上下文而不是初始化有问题
List<TCImages> tcImages;
Context context;
public TCAdapter(List<TCImages> tcImages, FragmentActivity activity){
this.tcImages = tcImages;
}
您是否为 Context context;
设置过值?
可能您想这样做:
public TCAdapter(List<TCImages> tcImages, Context context){
this.tcImages = tcImages;
this.context = context;
}