RecyclerView:未连接适配器;跳过布局和java.lang.NullPointerException

RecyclerView:未连接适配器;跳过布局和java.lang.NullPointerException,java,android,android-recyclerview,Java,Android,Android Recyclerview,当显示RecyclerView列表时,我不断收到错误“E/RecyclerView:未连接适配器;跳过布局””。我正确地从API获取了数据,但在空对象引用上获得了“void android.widget.TextView.setText(java.lang.CharSequence)”” 我知道这个问题以前被问过很多次,但我检查了大部分问题,没有一个对我有用 这是我的片段: public class HomeFragment extends Fragment { private View

当显示RecyclerView列表时,我不断收到错误“
E/RecyclerView:未连接适配器;跳过布局”
”。我正确地从API获取了数据,但在空对象引用上获得了“
void android.widget.TextView.setText(java.lang.CharSequence)”

我知道这个问题以前被问过很多次,但我检查了大部分问题,没有一个对我有用

这是我的片段:

public class HomeFragment extends Fragment {
    private View view;
    private RecyclerView recyclerView;
    private ArrayList<Result> arrayList;
    private SwipeRefreshLayout refreshLayout;
    private PostAdapter postAdapter;
    private MaterialToolbar toolbar;
    private SharedPreferences userPref;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view =  inflater.inflate(R.layout.layout_home,container,false);
        return view;

    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }
    private void init() {

        userPref = getContext().getApplicationContext().getSharedPreferences("user", Context.MODE_PRIVATE);
        recyclerView = (RecyclerView)view.findViewById(R.id.recyclerHome);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        refreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.swipeHome);
        toolbar =(MaterialToolbar) view.findViewById(R.id.toolbarHome);
        ((HomeActivity)getContext()).setSupportActionBar(toolbar);
        getResults();

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getResults();
            }
        });
    }

    private void getResults() {//Working Fine}
        
} 
公共类HomeFragment扩展了片段{
私人视野;
私人回收站;
私有ArrayList ArrayList;
私人交换机布局刷新布局;
私人后适配器后适配器;
私有材质工具栏;
私有共享引用userPref;
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气机。充气(右布局。布局\主,容器,假);
返回视图;
}
@凌驾
已创建视图上的公共void(视图,捆绑保存状态){
super.onViewCreated(视图,savedInstanceState);
init();
}
私有void init(){
userPref=getContext().getApplicationContext().GetSharedReferences(“用户”,Context.MODE_PRIVATE);
recyclerView=(recyclerView)view.findViewById(R.id.recyclerHome);
recyclerView.setHasFixedSize(true);
setLayoutManager(新的LinearLayoutManager(getContext());
refreshLayout=(SwipeRefreshLayout)view.findViewById(R.id.swipeHome);
工具栏=(MaterialToolbar)视图.findViewById(R.id.toolbarHome);
((HomeActivity)getContext()).setSupportActionBar(工具栏);
getResults();
refreshLayout.setOnRefreshListener(新的SwipeRefreshLayout.OnRefreshListener(){
@凌驾
公共void onRefresh(){
getResults();
}
});
}
私有void getResults(){//工作正常}
} 
我的适配器:

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostsHolder>{
    private ArrayList<Result> list;
    private Context context;

       public PostAdapter(Context context, ArrayList<Result> list) {
            this.list = new ArrayList<Result>(list);
            this.context = context;
    }

    @NonNull
    @Override
    public PostsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_home, parent, false);
        return new PostsHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PostsHolder holder, int position) {
        Result result = list.get(position);
        holder.imgPostDate.setText(result.getDate());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
   public static class PostsHolder extends RecyclerView.ViewHolder{
        private TextView imgPostProfile,imgPostName,imgPostDate;
        private ImageButton btnPostOption;

        public PostsHolder(@NonNull View itemView) {
            super(itemView);
            imgPostProfile =(TextView) itemView.findViewById(R.id.imgPostProfile);
            imgPostName = (TextView)itemView.findViewById(R.id.imgPostName);
            imgPostDate =(TextView) itemView.findViewById(R.id.imgPostDate);
            btnPostOption =(ImageButton) itemView.findViewById(R.id.btnPostOption);
        }
    }
}
公共类PostAdapter扩展了RecyclerView.Adapter{
私有数组列表;
私人语境;
公共PostAdapter(上下文、ArrayList列表){
this.list=新的ArrayList(列表);
this.context=上下文;
}
@非空
@凌驾
public PostsHolder onCreateViewHolder(@NonNull ViewGroup父级,int viewType){
View=LayoutFlater.from(parent.getContext()).flate(R.layout.layout\u home,parent,false);
返回新的PostsHolder(视图);
}
@凌驾
BindViewHolder上的公共无效(@NonNull PostsHolder holder,int位置){
结果=list.get(位置);
holder.imgPostDate.setText(result.getDate());
}
@凌驾
public int getItemCount(){
返回list.size();
}
公共静态类PostsHolder扩展了RecyclerView.ViewHolder{
私有文本视图imgPostProfile、imgPostName、imgPostDate;
私人图像按钮btnPostOption;
公共PostsHolder(@NonNull View itemView){
超级(项目视图);
imgPostProfile=(TextView)itemView.findViewById(R.id.imgPostProfile);
imgPostName=(TextView)itemView.findViewById(R.id.imgPostName);
imgPostDate=(TextView)itemView.findViewById(R.id.imgPostDate);
btnPostOption=(ImageButton)itemView.findViewById(R.id.btnPostOption);
}
}
}
我根据对类似问题的回答修改了代码,但没有一个有效

家庭布置图

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

    <com.google.android.material.appbar.MaterialToolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbarHome"
        android:elevation="1dp"
        android:theme="@style/Theme.MaCoSISBottomAppBar"
        app:title=""
        tools:targetApi="lollipop">
        <!--    <ImageView-->
        <!--        android:layout_width="100dp"-->
        <!--        android:layout_height="40dp"-->
        <!--        android:src="@drawable/logo_dark"/>-->
    </com.google.android.material.appbar.MaterialToolbar>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:id="@+id/swipeHome"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerHome"
            android:layout_height="match_parent"
            android:layout_width="match_parent">

        </androidx.recyclerview.widget.RecyclerView>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</LinearLayout>

邮政总局布局图

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:clickable="true"
    android:focusable="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dp">

            <TextView
                android:id="@+id/imgPostProfile"
                android:layout_width="60sp"
                android:layout_height="60sp"
                android:background="@drawable/shape_circle"
                android:text="MC"
                android:gravity="center"
                android:textColor="@color/colorWhite"
                android:textSize="14sp" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:paddingLeft="6dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imgPostName"
                    android:text="BBAD1252 Social Harmony and Business Skills Development"
                    android:textColor="@color/colorBlack"
                    android:textSize="14sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imgPostDate"
                    android:text="12 Novermber 2020"
                    android:textColor="@color/colorLightGrey"
                    android:textSize="12dp" />
            </LinearLayout>
            <ImageButton
                android:id="@+id/btnPostOption"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="8dp"
                android:background="@android:color/transparent"
                android:layout_gravity="center_vertical"
                android:src="@drawable/ic_baseline_more_vert_24"/>
        </LinearLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorLightGrey"
            />
    </LinearLayout>

</androidx.cardview.widget.CardView>

onCreateViewHolder
中,您夸大了错误的xml。应该是
R.layout.layout\u post

@NonNull
@Override
public PostsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_post, parent, false);
    return new PostsHolder(view);
}

onCreateViewHolder
中,您夸大了错误的xml。应该是
R.layout.layout\u post

@NonNull
@Override
public PostsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_post, parent, false);
    return new PostsHolder(view);
}

如果调用的是
getResults()
,那么调用setAdapter()就太迟了。您需要确保在
onCreateView()
期间设置了适配器,以便在绘制布局时不会跳过Recyclerview。

如果您的
getResults()
是在调用
Recyclerview.setAdapter()
的位置,则调用setAdapter太迟了。您需要确保在
onCreateView()期间设置了适配器,以便在绘制布局时不会跳过Recyclerview。

未连接适配器;跳过布局: 您应该在fragment的主线程方法中设置Adapter;然后向适配器添加或更新数据


settext[at adapter]上的空指针:在onCreateViewHolder中,您膨胀了错误的xml。它应该是R.layout.layou post而不是R.layout.layou home

没有连接适配器;跳过布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.MaterialToolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbarHome"
        android:elevation="1dp"
        android:theme="@style/Theme.MaCoSISBottomAppBar"
        app:title=""
        tools:targetApi="lollipop">
        <!--    <ImageView-->
        <!--        android:layout_width="100dp"-->
        <!--        android:layout_height="40dp"-->
        <!--        android:src="@drawable/logo_dark"/>-->
    </com.google.android.material.appbar.MaterialToolbar>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:id="@+id/swipeHome"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerHome"
            android:layout_height="match_parent"
            android:layout_width="match_parent">

        </androidx.recyclerview.widget.RecyclerView>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</LinearLayout>
您应该在fragment的主线程方法中设置Adapter;然后向适配器添加或更新数据


settext[at adapter]上的空指针:在onCreateViewHolder中,您膨胀了错误的xml。它应该是R.layout.layout\u post而不是R.layout.layout\u home

以防止错误
E/RecyclerView:未连接适配器;跳过布局
,您应该调用
recyclerView.setAdapter(适配器)
init()中方法

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

    <com.google.android.material.appbar.MaterialToolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbarHome"
        android:elevation="1dp"
        android:theme="@style/Theme.MaCoSISBottomAppBar"
        app:title=""
        tools:targetApi="lollipop">
        <!--    <ImageView-->
        <!--        android:layout_width="100dp"-->
        <!--        android:layout_height="40dp"-->
        <!--        android:src="@drawable/logo_dark"/>-->
    </com.google.android.material.appbar.MaterialToolbar>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:id="@+id/swipeHome"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerHome"
            android:layout_height="match_parent"
            android:layout_width="match_parent">

        </androidx.recyclerview.widget.RecyclerView>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</LinearLayout>

然后,在获得数据后,应通过调用
adapter.notifyDataSetChanged()通知适配器以显示数据或使用任何其他通知方法。检查其他通知方法的应答。

以防止错误
E/RecyclerView:未连接适配器;跳过布局
,您应该调用
recyclerView.setAdapter(适配器)
init()中方法

那么在你有了数据之后你应该