Problemas em pegar ID da view => view.id_selecionada

Eu sou iniciante em programação Mobile e quero adicionar em minha aplicação um TabLayout com ViewPager2, estou tendo o seguinte problema:

Eu tenho uma MainActivity e um MainFragment em kotlin:

MainActivity.kt

package com.gugaesmaltes.ellacosmeticos.view

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.gugaesmaltes.ellacosmeticos.R

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

    if (savedInstanceState == null) {
        supportFragmentManager.beginTransaction()
                .add(R.id.container, MainFragment())
                .commitNow()
    }
}

}

MainFragment.kt

package com.gugaesmaltes.ellacosmeticos.view
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayoutMediator
import com.gugaesmaltes.ellacosmeticos.R

class MainFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_main, container, false)

}
//val test = arrayListOf("A","B","C","D","E","F","G")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    TabLayoutMediator(view.tab_layout, view.frag_view_pager){

    }
}

}

fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.tabs.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<View
    android:id="@+id/view"
    android:layout_width="match_parent"
    android:layout_height="0.5dp"
    android:background="@android:color/darker_gray" />

<androidx.viewpager2.widget.ViewPager2
    android:id="@+id/frag_view_pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

O problema se encontra no MainFragment.kt, no TabLayoutMediator:

    TabLayoutMediator(view.tab_layout, view.frag_view_pager){

    }

Supostamente eu deveria conseguir chamar os IDs apenas com view.

*Eu já tentei seguir tutoriais da internet e não tive sucesso.
*Já tentei passar variáveis desta forma:
val tabLayout = view.findViewById(R.id.tab_layout)
val viewPager= view.findViewById(R.id.frag_view_pager)
TabLayoutMediator(tabLayout,viewPager){

           }

Desde já agradeço :slight_smile:

Qual erro deu aqui?

e:C:\Users\gusta\AndroidStudioProjects\EllaCosmeticos\app\src\main\java\com\gugaesmaltes\ellacosmeticos\view\MainFragment.kt: (20, 30): Type inference failed: Not enough information to infer parameter T in fun <T : View!> findViewById(p0: Int): T!
Please specify it explicitly.

https://developer.android.com/reference/com/google/android/material/tabs/TabLayoutMediator#TabLayoutMediator(com.google.android.material.tabs.TabLayout,%20androidx.viewpager2.widget.ViewPager2,%20com.google.android.material.tabs.TabLayoutMediator.TabConfigurationStrategy)
Não tá faltando um argumento?

Não por conta que o problema nessa situação específica se dá no fato que ele não consegue pegar o ID do TabLayout e do ViewPager2

Tenta assim:

val tabLayout: TabLayout = view.findViewById(R.id.tab_layout)
val viewPager: ViewPager2 = view.findViewById(R.id.frag_view_pager)

Fiz o que você falou, deu certo, só que agora o aplicativo abre e crasha

MainFragment.kt

package com.gugaesmaltes.ellacosmeticos.view

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gugaesmaltes.ellacosmeticos.R

class MainFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_main, container, false)
}

private val test = arrayListOf("A", "B", "C", "D", "E", "F", "G")

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val tabLayout: TabLayout = view.findViewById(R.id.tab_layout)
    val viewPager: ViewPager2 = view.findViewById(R.id.frag_view_pager)
    viewPager.adapter = FragmentTypeAdapter(this)


    TabLayoutMediator(tabLayout, viewPager) { tab, position ->
        tab.text = test[position] + position
    }.attach()
}

}

class FragmentTypeAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
override fun getItemCount(): Int {
return 7
}

override fun createFragment(position: Int): Fragment {
    return Fragment()
}

}

Está crashando com qual erro?

Foi! Esse problema era por conta de uma view dentro do fragment_main.xml que estava dando problema por algum motivo, deletei ela e foi. Valeu irmão!

Erro que dava da VIEW:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gugaesmaltes.ellacosmeticos, PID: 16160
android.view.InflateException: Binary XML file line #15 in com.gugaesmaltes.ellacosmeticos:layout/fragment_main: Binary XML file line #15 in com.gugaesmaltes.ellacosmeticos:layout/fragment_main: Error inflating class null
Caused by: android.view.InflateException: Binary XML file line #15 in com.gugaesmaltes.ellacosmeticos:layout/fragment_main: Error inflating class null
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘boolean java.lang.String.equals(java.lang.Object)’ on a null object reference
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1052)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at com.gugaesmaltes.ellacosmeticos.view.MainFragment.onCreateView(MainFragment.kt:19)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
at android.app.Activity.performStart(Activity.java:8024)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:58)
at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:5415)
at android.app.ActivityThread.access$3300(ActivityThread.java:237)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2076)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process: Sending signal. PID: 16160 SIG: 9

No fim das contas eu coloquei as variáveis:
val tabLayout: TabLayout = view.findViewById(R.id.tab_layout)
val viewPager: ViewPager2 = view.findViewById(R.id.frag_view_pager)

E tirei a view do arquivo fragment_main.xml