0

I want to write a good chat application with bottom navigation using Fragments. In ChatsFragment.kt I got a problem: enter image description here

My fun onCreateView() in ChatsFragment:

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    usersRecycler.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)

    var userlist = ArrayList<User>()
    userlist.add(User("Squirrel", "https://wampi.ru/image/RjQ3yNw"))
    userlist.add(User("Adam", "https://wampi.ru/image/RjQ3yNw"))


    var userAdapter = UserAdapter(requireActivity(), userlist)
    usersRecycler.adapter = userAdapter
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_chats, container, false)

}

Exception on usersRecycler.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)

I read a lot of solutions like: "replace "this" to requiredActivity() or requiredContext()", but nothing helped, throwing out this error: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference

I also wanted replace Fragments on Activity as bottom navigation. Would that be the wrong decision?

How can I resolve this problem? Help me, android developers, please...

4 Answers4

1

In onCreateView method is called when Fragment should create its View object hierarchy, either dynamically or via XML layout inflation.

onViewCreated event is triggered soon after onCreateView(). Any view setup should occur here. E.g., view lookups and attaching view listeners.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // Setup any handles to view objects here
    usersRecycler = view.findViewById(R.id.rv)
    usersRecycler.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
}

OR

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    val view: View = inflater.inflate(R.layout.fragment_chats, container, false)
    usersRecycler = view.findViewById(R.id.recyclerView)
    usersRecycler.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
    return view
  }
Umesh
  • 21
  • 4
  • use of " this " will give you the same error. You can't use this with fragment. You need to get the getActivity() instance.so in kotlin you can either pass "activity" or you can use requireActivity(). – Rahul Goswami Jun 28 '22 at 14:05
0

userRecycler is not initialized, you need to call

RecyclerView userRecycler = findViewById(ID)

to solve your issue

  • I wrote as follows, but "this" still underlined in red ```val root = inflater.inflate(R.layout.fragment_home, container, false) val recyclerView: RecyclerView = root.findViewById(R.id.usersRecycler) recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false) ``` – Катерина Островская Jun 28 '22 at 12:16
  • can you please pass requireContext() instead of this @КатеринаОстровская – Tanishq Chawda Jun 28 '22 at 12:45
0

You need to initialize the RecyclerView

val recyclerView: RecyclerView = root.findViewById(R.id.recyclerView)

Also, LinearLayoutManager takes the context in its first argument. "this" points to fragment at that line, not the needed context. You should probably change it to:

usersRecycler.layoutManager = LinearLayoutManager(requireContext(), RecyclerView.VERTICAL, false)
Emre Temiz
  • 16
  • 1
  • 1
0

To solve this issue you need to pass the context of fragment. in Kotlin below code can solve your problem. Just pass "activity"

   val layoutManager = LinearLayoutManager(activity)

You can also user

 layoutManager = LinearLayoutManager(requireActivity())

Both will return the getActivity() instance.

check this

Please let me know if you still face the error will help you for sure.

Thanks

Rahul Goswami
  • 762
  • 6
  • 18