首页 > 解决方案 > 错误 E/RecyclerView:未连接适配器;在 holder.itemView.setOnClickListener 下实现意图时跳过布局问题

问题描述

我收到错误 E/RecyclerView:未连接适配器;在这里跳过布局是我的代码。除了意图方法之外,代码运行良好。配置文件活动没有回收者视图。当我单击启动意图时,它会在 logcat 中显示错误

2020-06-14 09:53:06.887 3521-3521/com.technolgiya.nitcalling11 E/RecyclerView:未连接适配器;跳过布局

 FirebaseRecyclerAdapter<Contacts,FindFriendsViewHolder> firebaseRecyclerAdapter
            = new FirebaseRecyclerAdapter<Contacts, FindFriendsViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull final FindFriendsViewHolder holder, final int position, @NonNull final Contacts model)
        {
            holder.userNameTxt.setText(model.getName());
            Picasso.get().load(model.getImage()).into(holder.profileImageView);

            String visit_user_id = getRef(position).getKey();
            Toast.makeText(FindPeopleActivity.this, "view on:" + visit_user_id+getRef(position), Toast.LENGTH_LONG).show();



            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holder.getAdapterPosition();
                     String visit_user_id = getRef(position).getKey();
                    Toast.makeText(FindPeopleActivity.this, "clicked on:" + visit_user_id, Toast.LENGTH_LONG).show();

                    Intent intent = new Intent (FindPeopleActivity.this, ProfileActivity.class);
                    intent.putExtra("visit_user_id", visit_user_id);
                    intent.putExtra("profile_image", model.getImage());
                    intent.putExtra("profile_name", model.getName());
                    startActivity(intent);

                }
            });
        }

这是我的回收站视图

    <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FindPeopleActivity">


    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout_find_people"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar_contacts"
            android:layout_width="match_parent"
            android:layout_height="60dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/search_user_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="find freiend..."
                    android:textColorHint="@android:color/white"
                    android:textAlignment="center"
                    android:layout_marginRight="16dp"
                    android:drawableLeft="@drawable/search"
                    android:textColor="@android:color/white"
                    android:layout_centerVertical="true"
                    >

                </EditText>


            </RelativeLayout>

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>


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




    </androidx.recyclerview.widget.RecyclerView>

</RelativeLayout>

这是我的 ProfileActivity.java

 package com.technolgiya.nitcalling11;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

public class ProfileActivity extends AppCompatActivity {

    private String receiverUserID="";
    private String receiverUserImage="";
    private String receiverUserName="";
    private ImageView background_profile_view;
    private TextView name_profile;
    private Button add_friend;
    private Button decline_friend_request;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        background_profile_view.findViewById(R.id.background_profile_view);
        name_profile = findViewById(R.id.name_profile);
        add_friend = findViewById(R.id.add_friend);
        decline_friend_request = findViewById(R.id.decline_friend_request);


        receiverUserID = getIntent().getExtras().get("visit_user_id").toString();
        receiverUserImage = getIntent().getExtras().get("profile_image").toString();
        receiverUserName = getIntent().getExtras().get("profile_name").toString();


        Toast.makeText(this, receiverUserID, Toast.LENGTH_SHORT).show();

        Picasso.get().load(receiverUserImage).into(background_profile_view);
        name_profile.setText(receiverUserName);
    }
}

这是我的 activity_profile.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ProfileActivity">



    <ImageView
        android:id="@+id/background_profile_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/profile_image"
        android:scaleType="centerCrop"
        >
    </ImageView>


    <TextView
        android:id="@+id/name_profile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="User name"
        android:textColor="@color/colorPrimary"
        android:textSize="30dp"
        android:maxLines="2"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_centerInParent="true"
        >
    </TextView>

    <Button
        android:id="@+id/add_friend"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:layout_below="@+id/name_profile"
        android:layout_marginTop="35dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginBottom="12dp"
        android:background="@color/colorPrimary"
        android:text="Add Friend"
        android:padding="5dp"
        >
    </Button>

    <Button
        android:id="@+id/decline_friend_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:layout_below="@+id/add_friend"
        android:layout_marginTop="1dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginBottom="12dp"
        android:background="@color/colorPrimary"
        android:text="Cancel Friend Request"
        android:padding="5dp"
        android:visibility="gone"
        >
    </Button>



</RelativeLayout>

这是 FindPeople_Activity 的完整代码

package com.technolgiya.nitcalling11;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;

public class FindPeopleActivity extends AppCompatActivity {


    private RecyclerView findFriendList;
    private EditText searchET;
    private String str = "";
    private DatabaseReference usersRef;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_people);

        usersRef = FirebaseDatabase.getInstance().getReference().child("Users");

        FirebaseRecyclerOptions<Contacts> options = null;

        searchET = findViewById(R.id.search_user_text);
        findFriendList = findViewById(R.id.find_friends_list);
        findFriendList.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

        searchET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charseq, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charseq, int start, int before, int count) {
                if (searchET.getText().toString().equals("")) {
                    Toast.makeText(FindPeopleActivity.this, "please write name to search", Toast.LENGTH_SHORT).show();

                } else {
                    str = charseq.toString();
                    onStart();
                }


            }


            @Override
            public void afterTextChanged(Editable charseq) {

            }
        });




    }

    @Override
    protected void onStart() {
        super.onStart();
        FirebaseRecyclerOptions<Contacts> options = null;
        if(str.equals(""))
        {
            options =
                    new FirebaseRecyclerOptions.Builder<Contacts>()
                    .setQuery(usersRef, Contacts.class)
                    .build();
        }

        else
        {
            options =
                    new FirebaseRecyclerOptions.Builder<Contacts>()
                            .setQuery(usersRef.orderByChild("name")
                                            .startAt(str)
                                            .endAt(str + "\uf8ff")
                                    , Contacts.class)
                            .build();
        }



         FirebaseRecyclerAdapter<Contacts,FindFriendsViewHolder> firebaseRecyclerAdapter
                = new FirebaseRecyclerAdapter<Contacts, FindFriendsViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull final FindFriendsViewHolder holder, final int position, @NonNull final Contacts model)
            {
                holder.userNameTxt.setText(model.getName());
                Picasso.get().load(model.getImage()).into(holder.profileImageView);

                String visit_user_id = getRef(position).getKey();
                Toast.makeText(FindPeopleActivity.this, "view on:" + visit_user_id+getRef(position), Toast.LENGTH_LONG).show();



                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        holder.getAdapterPosition();
                         String visit_user_id = getRef(position).getKey();
                        Toast.makeText(FindPeopleActivity.this, "clicked on:" + visit_user_id, Toast.LENGTH_LONG).show();


                        Intent intent = new Intent (FindPeopleActivity.this, ProfileActivity.class);
                        intent.putExtra("visit_user_id", visit_user_id);
                        intent.putExtra("profile_image", model.getImage());
                        intent.putExtra("profile_name", model.getName());
                        startActivity(intent);

                    }
                });
            }
            @NonNull
            @Override
            public FindFriendsViewHolder onCreateViewHolder(@NonNull ViewGroup p, int viewType)
            {
                View view = LayoutInflater.from(p.getContext()).inflate(R.layout.contact_design,p,false);
                FindFriendsViewHolder viewHolder = new FindFriendsViewHolder(view);
                return viewHolder;
            }
        };
      findFriendList.setAdapter(firebaseRecyclerAdapter);
      firebaseRecyclerAdapter.startListening();
    }



    public static class FindFriendsViewHolder extends RecyclerView.ViewHolder
    {

        TextView userNameTxt;
        Button videoCallBtn;
        ImageView profileImageView;
        RelativeLayout cardView;

        public FindFriendsViewHolder(@NonNull View itemView) {
            super(itemView);

            userNameTxt=itemView.findViewById(R.id.name_contact);
            videoCallBtn=itemView.findViewById(R.id.call_btn);
            profileImageView=itemView.findViewById(R.id.image_contact);
            cardView=itemView.findViewById(R.id.card_view1);

            videoCallBtn.setVisibility(View.GONE);
        }
    }


}

这是我的contacts.java

package com.technolgiya.nitcalling11;

public class Contacts
{
    String name,image,status,uid;

    public Contacts() {
    }

    public Contacts(String name, String image, String status, String uid) {
        this.name = name;
        this.image = image;
        this.status = status;
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }
}

这是activity_contacts.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout_contacts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar_contacts"
            android:layout_width="match_parent"
            android:layout_height="50dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Contacts"
                    android:textSize="20dp"
                    android:textStyle="bold"
                    android:textColor="@android:color/white"
                    android:layout_centerVertical="true"
                    >

                </TextView>

                <ImageView
                    android:id="@+id/find_people_btn"
                    android:layout_width="38dp"
                    android:layout_height="40dp"
                    android:layout_alignParentEnd="true"
                    android:layout_marginEnd="12dp"
                    android:src="@drawable/find_people"
                    android:tint="@android:color/white"
                    >

                </ImageView>

            </RelativeLayout>

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/contact_list"
        android:layout_below="@+id/app_bar_layout_contacts"
        android:orientation="vertical"
        android:layout_above="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">




    </androidx.recyclerview.widget.RecyclerView>





    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_nav_menu" />



</RelativeLayout>

更新:

我有:

Unable to start activity ComponentInfo{com.technolgiya.nitcalling11/com.technolgiya.nitcalling11.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference 

标签: androidfirebaseandroid-recyclerview

解决方案


请尝试在设置适配器之前开始监听 Firebase 数据,所以

代替

  findFriendList.setAdapter(firebaseRecyclerAdapter);
  firebaseRecyclerAdapter.startListening();

  firebaseRecyclerAdapter.startListening();
  findFriendList.setAdapter(firebaseRecyclerAdapter);

在您的 onStart() 方法中。

更新

Unable to start activity ComponentInfo{com.technolgiya.nitcalling11/com.technolgiya.nitcalling11.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference

在 ProfileActivity 中替换

background_profile_view.findViewById(R.id.background_profile_view);

background_profile_view = findViewById(R.id.background_profile_view); 

推荐阅读